设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1603|回复: 0

MATLAB技巧-sort和sortrows函数

[复制链接]

30

主题

220

金钱

355

积分

入门用户

发表于 2019-7-22 16:54:05 | 显示全部楼层 |阅读模式

1、sort函数
sort函数用于对数据进行排序,通过help sort命令,可以查找到sort函数的具体用法:
  1. Y = SORT(X,DIM,MODE)
  2. has two optional parameters.  
  3. DIM selects a dimension along which to sort.
  4. MODE selects the direction of the sort
  5.    'ascend' results in ascending order
  6.    'descend' results in descending order
  7. The result is in Y which has the same shape and type as X.
复制代码


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

对于矩阵
1.png

按行升序:
2.jpg

按列升序:
3.jpg

从上述的结果看出,sort函数会比较矩阵中的每一个元素,将行中的每一个元素或者列中的每一个元素按照升序排列。
若现在需要将矩阵按照行排序,可以任意指定排序比较的列。可以使用sortrows函数。


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


sortrows函数根据列col升序排序:
4.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表