前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MATLAB技巧——sort和sortrows函数

MATLAB技巧——sort和sortrows函数

作者头像
felixzhao
发布2018-03-14 16:23:11
1.6K0
发布2018-03-14 16:23:11
举报
文章被收录于专栏:null的专栏

1、sort函数

sort函数用于对数据进行排序,通过help sort命令,可以查找到sort函数的具体用法:

代码语言:javascript
复制
Y = SORT(X,DIM,MODE)
has two optional parameters.  
DIM selects a dimension along which to sort.
MODE selects the direction of the sort
   'ascend' results in ascending order
   'descend' results in descending order
The result is in Y which has the same shape and type as X.

上面的意思是说,在sort函数中,有两个参数,一个参数是dim,dim表示的是按照哪一维排序,如行为1,列为2;第二个参数是mode,mode表示的是按照降序或者升序排列(缺省的时候是升序排列)。

对于矩阵

A=\begin{pmatrix} 5 & 7 & 8 \\ 4 & 6 & 1 \\ 8 & 0 & 7 \end{pmatrix}

按行升序:

这里写图片描述
这里写图片描述

按列升序:

这里写图片描述
这里写图片描述

从上述的结果看出,sort函数会比较矩阵中的每一个元素,将行中的每一个元素或者列中的每一个元素按照升序排列。

若现在需要将矩阵按照行排序,可以任意指定排序比较的列。可以使用sortrows函数。

2、sortrows函数

代码语言:javascript
复制
SORTROWS(X,COL) sorts the matrix based on the columns specified in the
vector COL.  If an element of COL is positive, the corresponding column
in X will be sorted in ascending order; if an element of COL is negative,
the corresponding column in X will be sorted in descending order. For 
example, SORTROWS(X,[2 -3]) sorts the rows of X first in ascending order 
for the second column, and then by descending order for the third
column.

sortrows函数根据列col升序排序:

这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、sort函数
  • 2、sortrows函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档