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

1 次查看(过去 30 天)
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.

采纳的回答

Kirby Fears
Kirby Fears 2016-2-16
编辑:Kirby Fears 2016-2-16
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.
  7 个评论
lauuser1
lauuser1 2016-2-17
Thank you. I'll definitely take a look at that... However what I'm looking for is that all values of A would need to be moved to the third row of Matrix B because the max value of A was in the third row. If the max value of A was in the first row, I would need to move all values of A to the first row of Matrix B.
I understand if I was simply moving all values of A to a column or row I would simply use what you mentioned above. However, I need to place it in a certain location depending on the location of the max value.

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2016-2-16
Well, much is undefined but the indexing is pretty simple...
B(A==1,icol)=sum(A);
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by