Question

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
Was it helpful?

Solution

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

OTHER TIPS

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`;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top