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
2013-4-22
[temp, order] = sort(a(2,:));
answer = a(:,order);
3 个评论
Saeid
2025-6-15
Neat!
Akira Agata
2025-6-20
Another solution:
answer = sortrows(a', 2)';
Stephen23
2025-6-22
answer = sortrows(a.', 2).';
Using transpose, rather than complex conjugate transpose.
类别
在 帮助中心 和 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!