如何在 MATLAB 中根据一列对二维数组进行排序? [英] How can I sort a 2-D array in MATLAB with respect to one column?
本文介绍了如何在 MATLAB 中根据一列对二维数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据特定列对矩阵进行排序.有一个 sort
函数,但它独立地对所有列进行排序.
I would like to sort a matrix according to a particular column. There is a sort
function, but it sorts all columns independently.
例如,如果我的矩阵 data
是:
For example, if my matrix data
is:
1 3
5 7
-1 4
那么所需的输出(按第一列排序)将是:
Then the desired output (sorting by the first column) would be:
-1 4
1 3
5 7
但是sort(data)
的输出是:
-1 3
1 4
5 7
如何按第一列对这个矩阵进行排序?
How can I sort this matrix by the first column?
推荐答案
我认为 sortrows 函数正是您要找的.p>
I think the sortrows function is what you're looking for.
>> sortrows(data,1)
ans =
-1 4
1 3
5 7
这篇关于如何在 MATLAB 中根据一列对二维数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文