sort
Sort elements of symbolic arrays
Description
sorts
the elements of Y
= sort(X
)X
in ascending lexicographical order.
If
X
is a vector, thensort(X)
sorts the vector elements ofX
.If
X
is a matrix, thensort(X)
treats the columns ofX
as vectors and sorts each column independently.If
X
is a multidimensional array, thensort(X)
operates along the first array dimension whose size does not equal 1, treating the elements as vectors.
[
also returns a collection of index vectors for any of the previous syntaxes.
Y
,I
] =
sort(___)I
is the same size as X
and describes the
arrangement of the elements of X
into Y
along
the sorted dimension. For example, if X
is an
m
-by-n
matrix and you sort the elements of
each column (dim = 1
), then each column of I
is an index vector of the sorted column of X
, such that
for j = 1:n Y(:,j) = X(I(:,j),j); end
Examples
Input Arguments
Output Arguments
Tips
Calling
sort
for arrays of numbers that are not symbolic objects invokes the MATLAB®sort
function.The
sort
function sorts symbolic complex numbers differently from MATLAB floating-point complex numbers. For symbolic inputX
that contains complex numbers,sort(X)
sorts the complex numbers first by their real parts, then by their imaginary parts to break ties. For floating-point inputX
, by default,sort(X)
sorts complex numbers by their magnitude, followed by their phase angles in the interval (−π, π] to break ties.