【 MATLAB 】sort ( Sort array elements )

举报
李锐博恩 发表于 2021/07/15 05:39:20 2021/07/15
【摘要】 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...

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

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


  
  1. clc
  2. clear
  3. close all
  4. A = [9 0 -7 5 3 8 -10 4 2];
  5. B = sort(A)

结果:

B =

   -10    -7     0     2     3     4     5     8     9
 


Sort Matrix Rows in Ascending Order

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


  
  1. clc
  2. clear
  3. close all
  4. % Create a matrix and sort its columns in descending order.
  5. A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]
  6. % A = 4×4
  7. %
  8. % 10 -12 4 8
  9. % 6 -9 8 0
  10. % 2 3 11 -2
  11. % 1 1 9 3
  12. %
  13. 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®字典顺序对字符串数组的每列中的字符串进行排序。


  
  1. clc
  2. clear
  3. close all
  4. % Starting in R2016b, you can create string arrays using the string function,
  5. % and sort them using the sort function. Sort strings in each column of a string array according to Unicode® dictionary order.
  6. A = string({'Smith','Burns';...
  7. 'Jones','Matthews';...
  8. 'Peterson','Adams'});
  9. B = sort(A)
  10. % B = 3x2 string array
  11. % "Jones" "Adams"
  12. % "Peterson" "Burns"
  13. % "Smith" "Matthews"
  14. %
  15. % Sort the strings in each row.
  16. B = sort(A,2)
  17. % B = 3x2 string array
  18. % "Burns" "Smith"
  19. % "Jones" "Matthews"
  20. % "Adams" "Peterson"
  21. %

结果:

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数组的索引


  
  1. clc
  2. clear
  3. close all
  4. % 创建一个datetime值数组,并按升序对其进行排序,即从最早的日历日期到最晚的日历日期。
  5. ds = {'2012-12-22';'2063-04-05';'1992-01-12'};
  6. A = datetime(ds,'Format','yyyy-MM-dd')
  7. % A = 3x1 datetime array
  8. % 2012-12-22
  9. % 2063-04-05
  10. % 1992-01-12
  11. [B,I] = sort(A)
  12. % B = 3x1 datetime array
  13. % 1992-01-12
  14. % 2012-12-22
  15. % 2063-04-05
  16. % I = 3×1
  17. %
  18. % 3
  19. % 1
  20. % 2
  21. % B lists the sorted dates and I contains the corresponding indices of A.
  22. % Access the sorted elements from the original array directly by using the index array I.
  23. A(I)
  24. % ans = 3x1 datetime array
  25. % 1992-01-12
  26. % 2012-12-22
  27. % 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


  
  1. clc
  2. clear
  3. close all
  4. % Create a 2-by-2-by-2 array and sort its elements in ascending order along the third dimension.
  5. A(:,:,1) = [2 3; 1 6];
  6. A(:,:,2) = [-1 9; 0 12];
  7. A
  8. % A =
  9. % A(:,:,1) =
  10. %
  11. % 2 3
  12. % 1 6
  13. %
  14. %
  15. % A(:,:,2) =
  16. %
  17. % -1 9
  18. % 0 12
  19. B = sort(A,3)
  20. % B =
  21. % B(:,:,1) =
  22. %
  23. % -1 3
  24. % 0 6
  25. %
  26. %
  27. % B(:,:,2) =
  28. %
  29. % 2 9
  30. % 1 12
  31. %使用A(:)的列表示来排序A的所有元素。
  32. B = sort(A(:))
  33. % B = 8×1
  34. %
  35. % -1
  36. % 0
  37. % 1
  38. % 2
  39. % 3
  40. % 6
  41. % 9
  42. % 12

Complex Vector


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

 

 

 

 

 

 

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/83118459

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。