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

1 view (last 30 days)
For example, vector A is a 3x1 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 3x5 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 (3x1) 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.

Accepted Answer

Kirby Fears
Kirby Fears on 16 Feb 2016
Edited: Kirby Fears on 16 Feb 2016
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.

Sign in to comment.

More Answers (1)

dpb on 16 Feb 2016
Well, much is undefined but the indexing is pretty simple...
Now, you'll have to iterate or otherwise set the column index and regenerate A and it's presumed there will only be a single value of '1' in A (else't you'll get more than one value set in B or none if didn't happen to have any). Also A must be a vector of length<=size(B,1) or you'll also be changing the size of the B array.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by