For ex;I have an array A=[1:4] and B=[12:19:2:20].If I sort B in descending order B=[20 19 12 2].I want the corresponding positions in A to be swapped as well...ie A=[4 2 1 3].How do I achieve that?

 채택된 답변

Matt Tearle
Matt Tearle 2011년 4월 3일

1 개 추천

[sortB,idx] = sort(B,'descend');
sortA = A(idx);
Or put the vectors into a matrix and use sortrows.

댓글 수: 2

Anand Anand
Anand Anand 2011년 4월 3일
thanks a lot Matt..I was actually writing a very long algorithm.-:)
Matt Tearle
Matt Tearle 2011년 4월 7일
In MATLAB, that's generally a sign you're doing it wrong! :)

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by