I am having trouble indexing

조회 수: 1 (최근 30일)
Leeba Ann Chacko
Leeba Ann Chacko 2022년 6월 6일
댓글: Leeba Ann Chacko 2022년 6월 6일
I have two 2x4 matrices A and B. I have rearranged A in decending order and have obtained the index for this rearrangement. I would like to use this index to rearrange B. However, when I try that, it is is only working for the 1st column and using the values from this column for the rest. How do I rectify this?
Here is my code:
A = rand(2,4);
B = rand(2,4);
[C,idx] = sort(A,'descend');
D=B(idx);

채택된 답변

KSSV
KSSV 2022년 6월 6일
A = rand(2,4);
B = rand(2,4);
[C,idx] = sort(A,'descend');
D=B;
for i = 1:size(idx,2)
D(:,i) = B(idx(:,i),i) ;
end
  댓글 수: 1
Leeba Ann Chacko
Leeba Ann Chacko 2022년 6월 6일
Thank you, this worked well for me! :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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