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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Kirby Fears
Kirby Fears am 16 Feb. 2016
Bearbeitet: Kirby Fears am 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.
  7 Kommentare
lauuser1
lauuser1 am 17 Feb. 2016
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 16 Feb. 2016
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.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by