Sort a matrix based on one row?

조회 수: 10 (최근 30일)
Umesh
Umesh 2013년 4월 22일
댓글: Stephen23 2025년 6월 22일
lets say we have a 3x3 matrix as
a= 2 5 3;
2 8 1;
4 1 9;
and I want this matrix sorted based on its 2nd row in ascending order i.e.
answer= 3 2 5;
1 2 8;
9 4 1;
Thank you for any help you can offer.

답변 (1개)

Walter Roberson
Walter Roberson 2013년 4월 22일
[temp, order] = sort(a(2,:));
answer = a(:,order);
  댓글 수: 3
Akira Agata
Akira Agata 2025년 6월 20일
Another solution:
answer = sortrows(a', 2)';
Stephen23
Stephen23 2025년 6월 22일
answer = sortrows(a.', 2).';
Using transpose, rather than complex conjugate transpose.

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

카테고리

Help CenterFile 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