sort a 2x4 matrix
3 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
采纳的回答
Adam Danz
2019-5-9
[~, sortIdx] = sort(V(1,:));
Iwant = V(:,sortIdx);
2 个评论
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
2019-5-9
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');
0 个评论
另请参阅
类别
在 Help Center 和 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!