Matlab数据处理之排序函数sort

文章目录

展开
4.7
(3)

今天带来Matlab中一个可以给数组中元素自动排序的函数,即,sort函数。本文,将从sort函数基本用法、sort函数用法介绍、sort函数实例等几个方面介绍sort函数。其中实例主要包括:按升序排序向量、按升序排序矩阵行、按降序对矩阵列排序、排序和索引日期时间数组、排序三维数组等。

Matlab数据处理之排序函数sort

sort函数帮助文档如下:

>> help sort
 sort   Sort in ascending or descending order.
    For vectors, sort(X) sorts the elements of X in ascending order.
    For matrices, sort(X) sorts each column of X in ascending order.
    For N-D arrays, sort(X) sorts along the first non-singleton
    dimension of X. When X is a cell array of strings, sort(X) sorts
    the strings in ASCII dictionary order.
 
    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.
 
    [Y,I] = sort(X,DIM,MODE) also returns an index matrix I.
    If X is a vector, then Y = X(I).  
    If X is an m-by-n matrix and DIM=1, then
        for j = 1:n, Y(:,j) = X(I(:,j),j); end
 
    When X is complex, the elements are sorted by ABS(X).  Complex
    matches are further sorted by ANGLE(X).
 
    When more than one element has the same value, the order of the
    elements is preserved in the sorted result and the indices 
    relating to equal elements will be ascending.
 
    Example: If X = [3 7 5
                     0 4 2]
 
    then sort(X,1) is [0 4 2  and sort(X,2) is [3 5 7
                       3 7 5]                   0 2 4];

sort函数基本用法

B = sort(A)
B = sort(A,dim)
B = sort(___,direction)
[B,I] = sort(___)

sort函数用法介绍

B=sort(A)沿大小不等于1的第一个数组维度按升序对A的元素进行排序。

如果A是向量,则sort(A)对向量元素进行排序。

如果A是矩阵,则sort(A)将A的列视为向量,并对每一列进行排序。

如果A是多维数组,则排序(A)沿着大小不等于1的第一个数组维度进行操作,将元素视为向量。

B=sort(A,dim)返回沿维度dim排序的A元素。例如,如果A是矩阵,则sort(A,2)对每行中的元素进行排序。

B=sort(_,direction)使用前面的任何语法按照direction指定的顺序返回A的排序元素。单个字符串“升序”表示升序(默认),“降序”表示降序。

[B,I]=sort(_)还返回前面任何语法的索引向量集合。I与A的大小相同,并描述了A的元素沿排序维度排列到B中的情况。例如,如果A是数字向量,则B=A(I)。

sort函数实例

按升序排序向量

创建一个行向量并按升序对其元素进行排序。

>> A = [9 0 -7 5 3 8 -10 4 2];
>> B = sort(A)

B =

   -10    -7     0     2     3     4     5     8     9

按升序排序矩阵行

创建一个矩阵,并按升序对其每一行进行排序。

>> A = [3 6 5; 7 -2 4; 1 0 -9]

A =

     3     6     5
     7    -2     4
     1     0    -9

>> B = sort(A,2)

B =

     3     5     6
    -2     4     7
    -9     0     1

按降序对矩阵列排序

创建矩阵并按降序对其列进行排序。

>> A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]

A =

    10   -12     4     8
     6    -9     8     0
     2     3    11    -2
     1     1     9     3

>> B = sort(A,'descend')

B =

    10     3    11     8
     6     1     9     3
     2    -9     8     0
     1   -12     4    -2
Matlab数据处理之排序函数sort

排序和索引日期时间数组

创建一个日期时间值数组,并按升序排序,即从最早到最晚的日历日期。

>> ds = {'2012-12-22';'2063-04-05';'1992-01-12'};
A = datetime(ds,'Format','yyyy-MM-dd')

A = 

   2012-12-22
   2063-04-05
   1992-01-12

>> [B,I] = sort(A)

B = 

   1992-01-12
   2012-12-22
   2063-04-05


I =

     3
     1
     2

B列出了排序的日期,I包含A的相应索引。

使用索引数组I直接访问原始数组中的排序元素。

>> A(I)

ans = 

   1992-01-12
   2012-12-22
   2063-04-05
Matlab数据处理之排序函数sort

排序三维数组

创建一个2乘2乘2的数组,并沿第三维度按升序对其元素进行排序。

>> A(:,:,1) = [2 3; 1 6];
>> A(:,:,2) = [-1 9; 0 12];
>> A

A(:,:,1) =

     2     3
     1     6


A(:,:,2) =

    -1     9
     0    12

>> B = sort(A,3)

B(:,:,1) =

    -1     3
     0     6


B(:,:,2) =

     2     9
     1    12

使用A(:),A的列表示法,对A的所有元素进行排序。

>> B = sort(A(:))

B =

    -1
     0
     1
     2
     3
     6
     9
    12

共计3人评分,平均4.7

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/program/matlab/sort-function-usage.html

(1)
上一篇 2022年12月20日 21:25
下一篇 2022年12月25日 14:10

你可能感兴趣的文章

  • Matlab双对数刻度绘图函数loglog

    文章目录 展开常见用法语法说明绘制一个线条绘制多个线条指定轴标签和刻度值将点绘制为不带线的标记添加图例仅指定 y 坐标指定目标坐标区绘图后更改线特性 4 (2)…

    2020年10月24日
    0688
  • Matlab二维绘图函数plot用法

    文章目录 展开常见语法单线条绘制多线条绘制二维数组绘图指定线型指定线型、颜色和标记在特定的数据点显示标记指定线宽、标记大小和标记颜色添加标题和轴标签绘制持续时间并指定刻度格式指定线…

    2020年10月17日
    0327
  • Matlab快速入门之文本和字符

    文章目录 展开字符串数组中的文本字符数组中的数据 4.6 (5) 本文中,将会讲解Matlab中关于文本和字符的创建。其中,文本指的是字符串数组中的文本;字符指的是字符数组中的数据…

    2022年09月07日
    0285
  • Matlab使用switch来判断执行哪条语句

    文章目录 展开常见用法语法说明比较单个值与多个值进行比较友情提示 3.5 (4) 前文中,我们说了Matlab使用for编写循环语句,使用if编写判断语句,使用while编写循环执…

    2021年04月11日
    0466
  • Matlab文字云图创建函数wordcloud

    文章目录 展开常见用法语法说明使用表创建文字云准备文本数据以创建文字云指定单词大小指定单词颜色使用 Text Analytics Toolbox 创建文字云 4…

    2020年11月20日
    0128
  • Matlab streamtube函数创建三维流管图

    文章目录 展开常见用法语法说明以可视方式呈现流使用顶点数据和发散性以可视方式呈现流 4 (2) 今天,再来一篇关于Matlab 流线图系列教程。本文,我们讲解下Matlab使用st…

    2021年03月08日
    0195

发表回复

登录后才能评论
本站APP
微信小程序