1. 程式人生 > >【 MATLAB 】sort ( Sort array elements )

【 MATLAB 】sort ( Sort array elements )

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列中的元素進行排序。

  • sort(A,2)對A行中的元素進行排序。

如果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 ⋯