MATLAB: How to select the row of an element in a vector and put it in the same row for a different matrix

matrixrowrowsvector

For example, vector A is a 3×1 and has the value 1 in its third row. I want to then take the sum of the entire vector A and put this sum in a 3×5 matrix B (currently filled with NaN's) in the first column third row. I want to keep doing this for each subsequent column– so for a second round, if vector A (3×1) has the value 1 in the first row, I want to put the sum of all numbers into the second column first row of matrix B.

Best Answer

Here's an example of creating an index and using it appropriately:
A = [1;2;3];
B = NaN(3,5);
% find where A is equal to 1
idx1 = (A==1);
% Set B's column 1 to be the sum(A) in the same row where A is 1
B(idx1,1) = sum(A);
Hope this helps.