(1)B=sort(A) Sort the one-dimensional or two-dimensional array in ascending order and return the sorted array. When A is two-dimensional, sort each column of the array.
eg: A=[1,5,3], then sort(A)=[1,3,5]
A=[1,5,3;2,4,1], then sort(A)=[1,4,1;2,5,3]
(2)B=sort(A,dim), sort the array in ascending order in the specified direction,
dim =1 means sorting each column, and dim=2 means sorting each row.
(3)B=sort(A,dim,mode), mode is the specified sorting mode. When the mode is "ascend", the ascending order is performed. When the mode is "descend", the descending order is performed.
(4)[B,I]=sort(A,...), I is the row position or column position of the returned sorted element in the original array.
Some examples:
>> 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
The above content briefly introduces the usage of the sort function in matlab. I hope it will help you. For more knowledge about the sort function, please log on to the official website of Script House to learn more!