필터 지우기
필터 지우기

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

조회 수: 3 (최근 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에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by