sort
Sorts elements of x into ascending order by default.
Syntax
s = sort(x)
s = sort(x)
s = sort(x, dim)
s = sort(x, 'ascend')
s = sort(x, 'descend')
s = sort(x, dim, 'ascend')
s = sort(x, dim, 'descend')
[s, idx] = sort(...)
Inputs
- x
- A matrix, an array of char, or a cell.
- dim
- The dimension on which to sort
Outputs
- s
- Sorted elements.
- idx
- Index vector such that s = x(idx).
Examples
One output with default dimension and direction input:
R = sort([4,3,2,1])
R = [Matrix] 1 x 4
1 2 3 4
Two outputs with default dimension and direction:
[s,idx]=sort([4,3,2,1])
s = [ 1 2 3 4 ]
idx = [ 4 3 2 1 ]
Two outputs with ascend specified:
[s,idx]=sort([1,2,3,4],'ascend')
s = [ 1 2 3 4 ]
idx = [ 1 2 3 4 ]
Two outputs with dim and descend specified:
[s,idx]=sort([4,3,2,1;3,6,1,2;2,2,2,1],2,'descend')
s = [ 4 3 2 1 ; 6 3 2 1 ; 2 2 2 1 ]
idx = [ 1 2 3 4 ; 2 1 4 3 ; 1 2 3 4 ]
Comments
The default sort direction is ascend.
NaN values are sorted to the high end of the list, as if they are greater than all other values, including inf.