如何在MATLAB中针对一列对二维数组排序? [英] How can I sort a 2-D array in MATLAB with respect to one column?

查看:201
本文介绍了如何在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 功能就是您想要的.

I think the sortrows function is what you're looking for.

>> sortrows(data,1)

ans =

    -1     4
     1     3
     5     7

这篇关于如何在MATLAB中针对一列对二维数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆