Main Content

sort

Sort elements of symbolic arrays

Description

Y = sort(X) sorts the elements of X in the default ascending order.

  • If X is a vector, then sort(X) sorts the vector elements of X.

  • If X is a matrix, then sort(X) treats the columns of X as vectors and sorts each column independently.

  • If X is a multidimensional array, then sort(X) operates along the first array dimension whose size does not equal 1, treating the elements as vectors.

example

Y = sort(X,dim) sorts the elements of X along the dimension dim. For example, if X is a two-dimensional matrix, then sort(X,1) sorts the elements of each column of X, and sort(X,2) sorts the elements of each row.

example

Y = sort(___,direction) sorts the elements of X in the order specified by direction using any of the previous syntaxes. "ascend" indicates ascending order (the default), and "descend" indicates descending order.

example

[Y,I] = sort(___) also returns a collection of index vectors for any of the previous syntaxes. 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

example

Examples

collapse all

Create a symbolic row vector X and sort its elements in ascending order. When X contains symbolic numbers and variables, then sort(X) returns the sorted numbers followed by the sorted variables.

syms a b c d e
X = [7 e 1 c 5 d a b]
X = (7e1c5dab)
sort(X)
ans = (157abcde)

When sorting the elements of a matrix, sort can work along the columns or rows of that matrix.

Create a symbolic matrix.

X = magic(3)*sym("a")
X = 

(8aa6a3a5a7a4a9a2a)

Sort the matrix. By default, the sort command sorts the elements of each column.

Y = sort(X)
Y = 

(3aa2a4a5a6a8a9a7a)

To sort the elements of each row, set the value of the dim option to 2.

Y = sort(X,2)
Y = 

(a6a8a3a5a7a2a4a9a)

Create a symbolic matrix.

X = magic(3)*sym("a")
X = 

(8aa6a3a5a7a4a9a2a)

Sort the elements of each row in descending order.

Y = sort(X,2,"descend")
Y = 

(8a6aa7a5a3a9a4a2a)

To find the indices that each element of a sorted matrix Y had in the original matrix X, call sort with two output arguments.

Create a symbolic matrix X.

X = magic(3)*sym("a")
X = 

(8aa6a3a5a7a4a9a2a)

Sort each column of X and return the indices of the sorted elements as I. Each column of I contains the presorted positions of elements in Y.

[Y,I] = sort(X)
Y = 

(3aa2a4a5a6a8a9a7a)

I = 3×3

     2     1     3
     3     2     1
     1     3     2

Since R2020b

Create a 2-by-2-by-2 symbolic array that contains symbolic numbers, variables, and functions.

syms x y f(x)
X(:,:,1) = [y 1; 1/3 x];
X(:,:,2) = [x -2; 1/4 f(x)];
X
X(:,:,1) = 

(y113x)

X(:,:,2) = 

(x-214f(x))

Sort its elements in ascending order along the third dimension.

Y = sort(X,3)
Y(:,:,1) = 

(x-214x)

Y(:,:,2) = 

(y113f(x))

Use X(:), the column representation of X, to sort all of the elements of X.

Y = sort(X(:))
Y = 

(-214131xxyf(x))

Since R2024b

Sort a symbolic vector that contains real and complex numbers. The sort function sorts the real and complex numbers together, considering the magnitude of the numbers, followed by their phase angles in the interval (−π, π] to break ties.

X = sym([6 -1/2 3+4i 5i 4+3i])
X = 

(6-123+4i5i4+3i)

Y = sort(X)
Y = 

(-124+3i3+4i5i6)

Since R2024b

Create a vector that contains the irrational number π and other rational numbers that approximate π.

X = [sym(pi), sym(22)/7, 333/106, 223/71]
X = 

(π22733310622371)

Sort these numbers in ascending order.

Y = sort(X)
Y = 

(22371333106π227)

For reference, display the sorted result in decimal representation with a precision of six significant digits using vpa.

Yvpa = vpa(Y,6)
Yvpa = (3.140853.141513.141593.14286)

Input Arguments

collapse all

Input array, specified as a symbolic vector, matrix, or multidimensional array. sort uses the following rules: (since R2024b)

  • If X contains only symbolic numbers, then sort(X) sorts the elements numerically. sort evaluates symbolic numbers internally and aims to sort the evaluated numbers without round-off errors. For complex numbers, sort(X) sorts the numbers first by their magnitude, followed by their phase angles in the interval (−π, π] to break ties.

  • If X contains only symbolic variables, then sort(X) sorts the variable names first by their alphabetical components and then by their numerical components. For example, for X = [x1, y, x10, x2], the vector sorted in ascending order is [x1, x2, x10, y], instead of [x1, x10, x2, y].

  • If X contains symbolic numbers and variables, then sort(X) returns the sorted numbers followed by the sorted variables.

  • If X contains symbolic numbers, expressions, and functions, comparing and sorting the elements can be computationally complex. Therefore, sort uses internal sorting rules to optimize its performance.

Dimension to operate along, specified as a positive integer scalar. If you do not specify a value, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array X:

  • sort(X,1) sorts the elements in the columns of X.

    sort(X,1) column-wise operation

  • sort(X,2) sorts the elements in the rows of X.

    sort(X,2) row-wise operation

sort returns X if dim is greater than ndims(X).

Sorting direction, specified as "ascend" or "descend".

Output Arguments

collapse all

Sorted array, returned as a symbolic vector, matrix, or multidimensional array. Y is the same size and type as X.

Sort index, returned as a symbolic vector, matrix, or multidimensional array. I is the same size as X. The index vectors are oriented along the same dimension that sort operates on. For example, if X is a 2-by-3 matrix, then [Y,I] = sort(X,2) sorts the elements in each row of X. The output I is a collection of 1-by-3 row index vectors that contain the presorted positions of each row of Y.

Tips

  • Calling sort on arrays of numbers that are not symbolic objects invokes the MATLAB® sort function.

Version History

Introduced before R2006a

expand all