Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Kirby Fears
Kirby Fears le 16 Fév 2016
Modifié(e) : Kirby Fears le 16 Fév 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 commentaires
lauuser1
lauuser1 le 17 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 16 Fév 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.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by