MATLAB Function Reference | ![]() ![]() |
Sort elements in ascending order
Syntax
B = sort(A) B = sort(A,dim) [B,INDEX] = sort(A,...)
Description
B = sort(A)
sorts the elements along different dimensions of an array, and arranges those elements in ascending order. A
can be a cell array of strings.
Real, complex, and string elements are permitted. For identical values in A
, the location in the input array determines location in the sorted list. When A
is complex, the elements are sorted by magnitude, and where magnitudes are equal, further sorted by phase angle on the interval . If
A
includes any NaN
elements, sort
places these at the end.
If A
is a vector, sort(A)
arranges those elements in ascending order.
If A
is a matrix, sort(A)
treats the columns of A
as vectors, returning sorted columns.
If A
is a multidimensional array, sort(A)
treats the values along the first non-singleton dimension as vectors, returning an array of sorted vectors.
B = sort(A,dim)
sorts the elements along the dimension of A
specified by a scalar dim
.
If dim
is a vector, sort
works iteratively on the specified dimensions. Thus, sort(A,[1 2])
is equivalent to sort(sort(A,2),1)
.
[B,INDEX] = sort(A,...)
also returns an array of indices. INDEX
is an array of size(A)
, each column of which is a permutation vector of the corresponding column of A
. If A
has repeated elements of equal value, indices are returned that preserve the original relative ordering.
See Also
max
, mean
, median
, min
, sortrows
![]() | smooth3 | sortrows | ![]() |