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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by