Sort a matrix based on one row?

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 日

5 投票

[temp, order] = sort(a(2,:));
answer = a(:,order);

3 件のコメント

Saeid
Saeid 2025 年 6 月 15 日
Neat!
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2013 年 4 月 22 日

コメント済み:

2025 年 6 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by