replace all numbers in a matrix - matlab

There are two matrices; the first one is my input matrix
and the second one ("renaming matrix") is used to replace the values of the first one
That is, looking at the renaming matrix; 701 must be replaced by 1,...,717 must be replaced by 10,etc.. such that the input matrix becomes as such
The ? values are defined but i didn't put them. The second column of the input matrix is already sorted(ascending order from top down) but the values are not consecutive(no "710": see first pic).
The question is how to get the output matrix(last pic) from the first two.

Looks to me like it's screaming for a sparse matrix solution. In matlab you can create a sparse matrix with the following command:
SM = sparse( ri, ci, val );
where ri is the row index of the non-zero elements, ci is the corresponding column index, and val is the values.
Let's call your input matrix IM and your lookup matrix LUM, then we construct the sparse matrix:
nr = size(LUM, 1);
SM = sparse( ones(nr, 1), LUM(:, 1), LUM(:, 2) );
Now we can get your result in a single line:
newMatrix = reshape(SM(1, IM), size(IM));
almost magic.
I didn't have a chance to check this tonight - but if it doesn't work exactly as described, it should be really really close...

If the values in the first column all appear in the second column, and if all you want is replace the values in the second column by 1..n and change the values in the first column accordingly, you can do all of this with a simple call to ismember:
%# define "inputMatrix" here as the first array in your post
[~,newFirstColumn] = ismember(inputMatrix(:,1),inputMatrix(:,2));
To create your output, you'd then write
outputMatrix = [newFirstColumn,(1:length(newFirstColumn))'];

If M is the original matrix and R is the renaming matrix, here's how you do it
N = M;
for n = 1:size(M,1)
N(find(M==R(n,1))) = R(n,2);
end
Note that in this case you're creating a new matrix N with the renamed values. You don't have to do that if you like.

Related

Access multiple elements and assign to each selected element a different value

I need to know if there is any efficient way of doing the following in MATLAB.
I have several big sparse matrices, the size of each one is roughly 9000000x9000000.
I need to access multiple element of such matrix and assign to each selected element a different value stored in another array. I'll give an example:
What I have:
SPARSE MATRIX of size 9000000x9000000
Matrix with the list of indexes and values I want to access, this is a matrix like this:
[row1, col1, value1;
row2, col2, value2;
...
rowN, colN, valueN]
Where N is the length of such matrix.
What I need:
Assign to the SPARSE MATRIX the corresponding value to the corresponding index, this is:
SPARSE_MATRIX(row1, col1) = value1
SPARSE_MATRIX(row2, col2) = value2
...
SPARSE_MATRIX(rowN, colN) = valueN
Thanks in advance!
EDIT:
Thank you to both for answering, I think I did not explain myself well, I'll try again.
I already have a large SPARSE MATRIX of about 9000000 rows x 9000000 columns, it is a SPARSE MATRIX filled with zeros.
Then I have another array or matrix, let's call it M with N number of rows, where N could take values from 0 to 9000000; and 3 columns. The first two columns are used to index an element of my SPARSE MATRIX, and the third column stores the value I want to transfer to the SPARSE MATRIX, this is, given a random row of M, i:
SPARSE_MATRIX(M(i, 1), M(i, 2)) = M(i, 3)
The idea is to do that for all the rows, I have tried it with common indexing:
SPARSE_MATRIX(M(:, 1), M(:, 2)) = M(:, 3)
Now I would like to do this assignation for all the rows in M as fast as possible, because if I use a loop or common indexing it takes ages (I am using a 7th Gen i7 processor with 16 GB of RAM). And I also need to keep the zeros in the SPARSE_MATRIX.
EDIT 2: SOLVED! Thank you Metahominid, I was not thinking through, but yes the sparse function does solve my problem, I just think my brain circuits were shortcircuited yesterday and was unable to see through it hahaha. Thank you to both anyway!
Regards!
You can construct a sparse matrix like this.
A = sparse(i,j,v)
S = sparse(i,j,v) generates a sparse matrix S from the triplets i, j,
and v such that S(i(k),j(k)) = v(k). The max(i)-by-max(j) output
matrix has space allotted for length(v) nonzero elements. sparse adds
together elements in v that have duplicate subscripts in i and j.
So you can simply construct the row vector, column vector and value vector.
I am answering in part because I cannot comment. You question seems a little confusing to me. The sparse() function in MATLAB does just this.
You can enter your arrays of indices and values directly into the interface, or declare a sparse matrix of zeros and set each individually.
Given your data format make three vectors, ROWS = [row1; ...; rown], COLS = [col1; ...; coln], and DATA = [val1; ... valn]. I am assuming that your size is the overall size of the full matrix and not the sparse portion.
Then
A = sparse(ROWS, COLS, DATA) will do just what you want. You can even specify the original matrix size.
A = sparse(ROWS, COLS, DATA, 90...., 90....).

How do I reshape a non-quadratic matrix?

I have a column vector A with dimensions (35064x1) that I want to reshape into a matrix with 720 lines and as many columns as it needs.
In MATLAB, it'd be something like this:
B = reshape(A,720,[])
in which B is my new matrix.
However, if I divide 35604 by 720, there'll be a remainder.
Ideally, MATLAB would go about filling every column with 720 values until the last column, which wouldn't have 720 values; rather, 504 values (48x720+504 = 35064).
Is there any function, as reshape, that would perform this task?
Since I am not good at coding, I'd resort to built-in functions first before going into programming.
reshape preserves the number of elements but you achieve the same in two steps
b=zeros(720*ceil(35604/720),1); b(1:35604)=a;
reshape(b,720,[])
A = rand(35064,1);
NoCols = 720;
tmp = mod(numel(A),NoCols ); % get the remainder
tmp2 = NoCols -tmp;
B = reshape([A; nan(tmp2,1)],720,[]); % reshape the extended column
This first gets the remainder after division, and then subtract that from the number of columns to find the amount of missing values. Then create an array with nan (or zeros, whichever suits your purpose best) to pad the original and then reshape. One liner:
A = rand(35064,1);
NoCols = 720;
B = reshape([A; nan(NoCols-mod(numel(A),NoCols);,1)],720,[]);
karakfa got the right idea, but some error in his code.
Fixing the errors and slightly simplifying it, you end up with:
B=nan(720,ceil(numel(a)/720));
B(1:numel(A))=A;
Create a matrix where A fits in and assingn the elemnent of A to the first numel(A) elements of the matrix.
An alternative implementation which is probably a bit faster but manipulates your variable b
%pads zeros at the end
A(720*ceil(numel(A)/720))=0;
%reshape
B=reshape(A,720,[]);

Matrix operation difficulty

I am having trouble in pushing back a vector into a matrix. Considering, N×N matrix called MyMatrix, where N = 10 which consists of zero and non-zero elements with the diagonal elements being all zero. There are 2 vectors: Positive_Vector of dimension 15 that consists of 10 non-zero elements extracted from MyMatrix and ConceptsVector consisting of N elements respectively. Suppose the vector consists of the follwing elements
Positive_Vector = [MyMatrix(1,2), MyMatrix(1,6), MyMatrix(2,5), MyMatrix(2,6), MyMatrix(2,10), MyMatrix(3,1), MyMatrix(4,10), MyMatrix(5,3), MyMatrix(5,9),MyMatrix(6,1),MyMatrix(6,7),MyMatrix(7,3),MyMatrix(7,4),MyMatrix(8,1),MyMatrix(8,3)];
Concepts = [0.6,0.1,0.0,0.2,0.8,0.33,0.21,0.5,0.11];
My problem is how can I update MyMatrix with a new vector New_Positive_vector consisting of the same dimension but different values of the elements, such that the following operation
C1 = Concepts*NewMyMatrix
can be performed?
This is how I extracted the Positive_Vector. Can somebody please show how to do the reverse ie push back the new elements of New_Positive_Vector into the the respective places in NewMyMatrix?
for ii = 1:10
for jj = 1:10
if (MyMatrix(ii,jj)~=0)
Positive_Vector = MyMatrix(ii,jj);
end
end
end
Your explanation is vague but I assume that you want to extract the non-zero (or positive) elements from a matrix, do some operations in these elements, and push back them into the original matrix. Then I suggest,
MyMatrix = (rand(5)>0.5).*rand(5);
[n,m,Positive_vector] = find(MyMatrix);
k = sub2ind(size(MyMatrix),n,m);
MyMatrix(k) = Positive_vector*2;
First line is for generating a random matrix with some zeros. Second line is to find the non-zero element of the matrix. If you just want the positive element, then you can modify it to find(MyMatrix > 0). Here, n and m are the sets of row and column numbers of non-zero elements, but I make that into a 1D indices in the third line. Fourth line is to apply some operation (this case multiply by 2) to the extracted vector and push back to the original places in the original matrix.
I assume that you do more complicated operation than multiplying the non-zero elements by 2. Otherwise you can do something like this...
MyMatrix = MyMatrix - (MyMatrix>0).*MyMatrix + (MyMatrix>0).*(MyMatrix)*2
Here is just a slightly simpler way of doing what ysakamoto does:
%// some test matrix
MyMatrix = (rand(5)>0.5).*rand(5);
%// Logical indices to the non-zero entries
inds = MyMatrix ~= 0;
%// Do operations on the non-zeros, and assign results back
MyMatrix(inds) = 2*MyMatrix(inds);
If I understand you correctly then you should do the following:
First of all, with the code you have provided for generating PositiveVector, you will get a single element and not a vector. I assume you want to generate it as follows:
[nonZeroRows,nonZeroCols]=find(MyMatrix~=0);
Positive_Vector=MyMatrix(sub2ind(size(MyMatrix),nonZeroRows,nonCols));
Suppose now you have a New_Positive_Vector which you want to insert into NewMyMatrix, you do it as follows:
NewMyMatrix=zeros(size(MyMatrix));
NewMyMatrix(sub2ind(size(NewMyMatrix),nonZeroRows,nonZeroCols))=`New_Positive_Vector`;

How to extract a row of a matrix

In every other language if I have a matrix, if I call a mono-dimensional index, the result will be an array.I don't know why in Matlab if you take a single index of a matrix, you'll get a single element, that's stupid.
Anyway in C:
mat[4][4];
mat[0] is an array.
In Matlab:
mat=[1 2; 3 4];
How do I take the first row of the matrix? mat(1) is 1, not [1 2].
EDIT: There is another problem, I have a problem with this function:
function str= split(string, del)
index=1;
found=0;
str=['' ; ''];
for i=1:length(string)
if string(i)==del
found=1;
index=1;
elseif found==1
str(2,index)=string(i);
index=index+1;
else
str(1,index)=string(i);
index=index+1;
end
end
end
This returns sometimes a matrix and sometimes an array.
For example if I use split('FF','.') I get 'FF' as result, but what if I want to return a matrix? I can't even choose the dimensione of the matrix, in this context a weak typed language is a big disvantage.
You have to say which columns you want. : stands for all indices in a dimension, so to take first row
mat(1,:)
It is not stupid, but useful. If you address a matrix with only one index, it implicitly gets converted to a vector. This gives you the option to use linear indices (see sub2ind).
This will extract the second row
vector = mat(2,:)
And This will extract the second column
vector = mat(:,2)
You can use
vector = mat(end,:)
To extract the last row
Hope this helps you
From Matrix Indexing in MATLAB:
When you index into the matrix A using only one subscript, MATLAB
treats A as if its elements were strung out in a long column vector,
by going down the columns consecutively
I just hope it doesn't look stupid to you anymore (along with the right answers from angainor and Marwan)

what the mean of the following MATLAB code

please help me to understand this code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
i can't find it in my book. thanks.
The code you posted can be made more readable using temporary variables:
a = (i-1)*j+1;
b = i*j;
val = y(:,a:b);
x(:,i) = mean( val, 2 )'; %# =mean( val' )
What exactly you do not understand? For meaning of mean , : and ' consult matlab help.
It would help if you said exactly what you don't understand, but here are a few tips:
if you have something like a(r,c), that means matrix a, row r, column c (always in this order). In other words, you should have two elements inside the brackets separated by a comma where the first represents the row, the second the column.
If you have : by itself in one of the sides of the comma, that means "all". Thus, if you had a(r,:), then you would have matrix a, row r, all columns.
If : is not alone in one of the sides of the comma, then it will mean "to". So if you have a(r, z:y), that means matrix a, row r, columns z to y.
Mean = average. The format of the function in Matlab is M = mean(A,dim). A will be the matrix you take the average (or mean) of, M will be the place where the results are going to go. If dim = 1, you will get a row vector with each element being the average of a column. If dim = 2 (as it is in your case), then you should get a column vector, with each element being the average of a row. Be careful, though, because at the end of your code you have ', which means transpose. That means that your column vector will be transformed into a row vector.
OK, so your code:
x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';
Start with the bit inside, that is
y(:,((i-1)*j+1):i*j)
So that is saying
matrix y(r,c)
where
r (row) is :, that is, all rows
c (column) is ((i-1)j+1):ij, that is, columns going from (i-1)j+1 until ij
Your code will then get the matrix resulting from that, which I called y(r,c), and will do the following:
mean( (y(r,c), 2 )
so get the result from above and take the mean (average) of each row. As your code has the ' afterwards, that is, you have:
mean( (y(r,c), 2 )'
then it will get the column vector and transform into a row vector. Each element of this row will be the average of a row of y(r,c).
Finally:
x(:,i) = mean( (y(r,c), 2 )';
means that the result of the above will be put in column i of matrix x.
Shouldn't this be x(i,:) instead?
The i-th column of the array x is the average of the i-th group of j columns of the array y.
For example, if i is 1 and j is 3, the 1st column of x is the average of the first three columns of y.