I have 2x4 matrix and would like to sort the first row from smallest number to largest number, but I would also like the second row to follow along.
V=[9 3 5 2; 30 49 22 354]
Iwant=[2 3 5 9; 354 49 22 30]

 채택된 답변

Adam Danz
Adam Danz 2019년 5월 9일

0 개 추천

[~, sortIdx] = sort(V(1,:));
Iwant = V(:,sortIdx);

댓글 수: 2

Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019년 5월 9일
Thank you! Just what I needed.
Adam Danz
Adam Danz 2019년 5월 9일
편집: Adam Danz 2019년 5월 9일
Glad I could help!
Star Strider also had a good idea to use sortrows(). I did a speed test between these two solutions and found that the one I proposed is ~2.7 times faster on average (100,000 repetitions of each; p<0.0001 wilcox rank sum test).

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

추가 답변 (1개)

Davide Figoli
Davide Figoli 2019년 5월 9일

0 개 추천

you could try doing:
V=[9 3 5 2; 30 49 22 354];
V_orderd=[];
V_ordered(1,:)=sort (V(1,:));
V_ordered(2,:)=sort(V(2,:),'descend');

카테고리

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

태그

질문:

2019년 5월 9일

편집:

2019년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by