Sort Matrix by rows
18 次查看(过去 30 天)
显示 更早的评论
I have a matrix which first row indicates index numbers and second indicated the data. For example it goes like this:
1 2 3 4 5 6
23 45 10 90 11 34
I want to sort these descending but I don't want to loose the corresponding index either.
4 2 6 1 5 3
90 45 34 23 11 10
I have a large amount of data so it needs to be efficient too. How can I do that?
0 个评论
采纳的回答
更多回答(3 个)
Geoff Hayes
2014-10-23
Ege - consider using sortrows to perform the above task
A = [1 2 3 4 5 6
23 45 10 90 11 34];
B = sortrows(A',-2)';
We transpose A so that we can sort on the second column. The negative indicates descending sort order. The result is then transposed to get the desired output as
B =
4 2 6 1 5 3
90 45 34 23 11 10
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!