MatLab排序函数
sort函数的调用格式:
sort(X) 功能:返回对向量X中的元素按列升序排列的新向量。
[Y, I] = sort(A, dim, mode) 功能:对矩阵A的各列或各行重新排序,I记录Y中的元素在排序前A中位置,其中dim指明读A的列还是行进行排序。若dim=1,则按列排序;若dim=2,则按行排序。mode为排序的方式,取值'ascend'为升序,'descend'为降序。
- >> a=[1 2 3;3 9 6;4 10 8 ; 4 0 7]
- a =
- 1 2 3
- 3 9 6
- 4 10 8
- 4 0 7
- >> sort(a)
- ans =
- 1 0 3
- 3 2 6
- 4 9 7
- 4 10 8
- >> sort(a,1)
- ans =
- 1 0 3
- 3 2 6
- 4 9 7
- 4 10 8
- >> sort(a,1,'descend')
- ans =
- 4 10 8
- 4 9 7
- 3 2 6
- 1 0 3
- >> sort(a,2)
- ans =
- 1 2 3
- 3 6 9
- 4 8 10
- 0 4 7
- >> sort(a,2,'descend')
- ans =
- 3 2 1
- 9 6 3
- 10 8 4
- 7 4 0
- >>
>> a=[1 2 3;3 9 6;4 10 8 ; 4 0 7]
a =
1 2 3
3 9 6
4 10 8
4 0 7
>> sort(a)
ans =
1 0 3
3 2 6
4 9 7
4 10 8
>> sort(a,1)
ans =
1 0 3
3 2 6
4 9 7
4 10 8
>> sort(a,1,'descend')
ans =
4 10 8
4 9 7
3 2 6
1 0 3
>> sort(a,2)
ans =
1 2 3
3 6 9
4 8 10
0 4 7
>> sort(a,2,'descend')
ans =
3 2 1
9 6 3
10 8 4
7 4 0
>>
- >> b=[2 5 7 3 9 1 6]
- b =
- 2 5 7 3 9 1 6
- >> [Y,I] = sort(b)
- Y =
- 1 2 3 5 6 7 9
- I =
- 6 1 4 2 7 3 5
- >>