Sort a matrix based on one row?

61 views (last 30 days)
Umesh
Umesh on 22 Apr 2013
Commented: Stephen23 on 22 Jun 2025 at 18:48
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.

Answers (1)

Walter Roberson
Walter Roberson on 22 Apr 2013
[temp, order] = sort(a(2,:));
answer = a(:,order);
  3 Comments
Akira Agata
Akira Agata on 20 Jun 2025 at 2:53
Another solution:
answer = sortrows(a', 2)';
Stephen23
Stephen23 on 22 Jun 2025 at 18:48
answer = sortrows(a.', 2).';
Using transpose, rather than complex conjugate transpose.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!