sort

Sort array elements


Syntax

​B = sort(A)​

​B = sort(A,dim)​

​B = sort(___,direction)​

​B = sort(___,Name,Value)​

​[B,I] = sort(___)​


Description

​B = sort(A)按升序对A的元素进行排序。​

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

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

​B = sort(___,direction)使用任何先前的语法按方向指定的顺序返回A的已排序元素。 'ascend'表示升序(默认值),'descend'表示降序。​

​B = sort(___,Name,Value)指定用于排序的附加参数。 例如,sort(A,'ComparisonMethod','abs')按大小对A的元素进行排序。​

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

意思就是B是排序后的向量,而I是B中的元素在A中的索引。


操作的维度,指定为正整数标量。 如果未指定任何值,则默认值为第一个大小不等于1的数组维度。

  • 考虑矩阵A. sort(A,1)对A列中的元素进行排序。

【 MATLAB 】sort ( Sort array elements )_数组

  • ​sort(A,2)对A行中的元素进行排序。​

【 MATLAB 】sort ( Sort array elements )_数组_02

​如果dim大于ndims(A),则返回A. 当A是cell数组时,不支持dim,即,sort仅沿大小不等于1的第一个数组维度操作。​


下面举例说明:

Sort Vector in Ascending Order

创建一个行向量,并升序排序:

clc
clear
close all

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

结果:

B =

   -10    -7     0     2     3     4     5     8     9



Sort Matrix Rows in Ascending Order

创建一个矩阵,并对其每列降序排序:

clc
clear
close all

% Create a matrix and sort its columns in descending order.

A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]
% A = 4×4
%
% 10 -12 4 8
% 6 -9 8 0
% 2 3 11 -2
% 1 1 9 3
%
B = sort(A,'descend')

结果:

A =

    10   -12     4     8

     6    -9     8     0

     2     3    11    -2

     1     1     9     3

B =

    10     3    11     8

     6     1     9     3

     2    -9     8     0

     1   -12     4    -2


Sort String Array

对字符串数组排序:

从R2016b开始,您可以使用字符串函数创建字符串数组,并使用sort函数对它们进行排序。 根据Unicode®字典顺序对字符串数组的每列中的字符串进行排序。

clc
clear
close all

% Starting in R2016b, you can create string arrays using the string function,
% and sort them using the sort function. Sort strings in each column of a string array according to Unicode® dictionary order.

A = string({'Smith','Burns';...
'Jones','Matthews';...
'Peterson','Adams'});
B = sort(A)
% B = 3x2 string array
% "Jones" "Adams"
% "Peterson" "Burns"
% "Smith" "Matthews"
%
% Sort the strings in each row.

B = sort(A,2)
% B = 3x2 string array
% "Burns" "Smith"
% "Jones" "Matthews"
% "Adams" "Peterson"
%

结果:

B = 

  3×2 string 数组

    "Jones"       "Adams"   

    "Peterson"    "Burns"   

    "Smith"       "Matthews"

B = 

  3×2 string 数组

    "Burns"    "Smith"   

    "Jones"    "Matthews"

    "Adams"    "Peterson"


Sort and Index​​datetime​​ Array

排序并获得datetime数组的索引

clc
clear
close all

% 创建一个datetime值数组,并按升序对其进行排序,即从最早的日历日期到最晚的日历日期。

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

[B,I] = sort(A)
% B = 3x1 datetime array
% 1992-01-12
% 2012-12-22
% 2063-04-05

% I = 3×1
%
% 3
% 1
% 2

% B lists the sorted dates and I contains the corresponding indices of A.

% Access the sorted elements from the original array directly by using the index array I.

A(I)
% ans = 3x1 datetime array
% 1992-01-12
% 2012-12-22
% 2063-04-05

结果:

A = 

  3×1 datetime 数组

   2012-12-22

   2063-04-05

   1992-01-12

B = 

  3×1 datetime 数组

   1992-01-12

   2012-12-22

   2063-04-05

I =

     3

     1

     2

ans = 

  3×1 datetime 数组

   1992-01-12

   2012-12-22

   2063-04-05



Sort 3-D Array

clc
clear
close all

% Create a 2-by-2-by-2 array and sort its elements in ascending order along the third dimension.

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

B = sort(A,3)
% B =
% B(:,:,1) =
%
% -1 3
% 0 6
%
%
% B(:,:,2) =
%
% 2 9
% 1 12

%使用A(:)的列表示来排序A的所有元素。

B = sort(A(:))
% B = 8×1
%
% -1
% 0
% 1
% 2
% 3
% 6
% 9
% 12

Complex Vector

clc
clear
close all

% Sort the elements of a complex vector by their real parts.
%对于具有相同实部的元素,sort会根据其虚部来打破关系。
A = [1+2i 3+i i 0 -i];
B = sort(A,'ComparisonMethod','real')
% B = 1×5 complex
%
% 0.0000 - 1.0000i 0.0000 + 0.0000i 0.0000 + 1.0000i 1.0000 + 2.0000i 3.0000 + 1.0000i ⋯