(1)B=sort(A) 对一维或二维数组进行升序排序,并返回排序后的数组,当A为二维时,对数组每一列进行排序.
eg: A=[1,5,3],则sort(A)=[1,3,5]
A=[1,5,3;2,4,1],则sort(A)=[1,4,1;2,5,3]
(2)B=sort(A,dim),对数组按指定方向进行升序排序,
dim =1,表示对每一列进行排序,,dim=2表示对每一行进行排序.
(3)B=sort(A,dim,mode),mode为指定排序模式,mode为"ascend"时,进行升序排序,为"descend "时,进行降序排序.
(4)[B,I]=sort(A,.....),I为返回的排序后元素在原数组中的行位置或列位置.
一些例子:
>> A=[3 4 2;1 5 3;4 7 1] A = 3 4 2 1 5 3 4 7 1 >> A(:) ans = 3 1 4 4 5 7 2 3 1 >> min(A(:)) ans = 1 >> max(A(:)) ans = 7 >> A A = 3 4 2 1 5 3 4 7 1 >> sort(A) ans = 1 4 1 3 5 2 4 7 3 >> A A = 3 4 2 1 5 3 4 7 1 >> sort(A(:)) ans = 1 1 2 3 3 4 4 5 7
>> sort(A,1) ans = 1 4 1 3 5 2 4 7 3 >> sort(A,2) ans = 2 3 4 1 3 5 1 4 7 >> sort(A,1,"descend") ??? sort(A,1,"descend") | Error: Missing variable or function. >> sort(A,1,'descend') ans = 4 7 3 3 5 2 1 4 1 >> [B,I]=sort(A) B = 1 4 1 3 5 2 4 7 3 I = 2 1 3 1 2 1 3 3 2
本文转自:http://blog.sina.com.cn/s/blog_75f0893501010ivf.html