Documentation Center

  • Trial Software
  • Product Updates

sort

Class: ordinal

Sort elements of ordinal array

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

Syntax

B = sort(A)
B = sort(A,dim)
B = sort(A,dim,mode)
[B,I] = sort(A,...)

Description

B = sort(A), when A is an ordinal vector, sorts the elements of A in ascending order. For ordinal matrices, sort(A) sorts each column of A in ascending order. For N-D ordinal arrays, sort(A) sorts the along the first nonsingleton dimension of A. B is an ordinal array with the same levels as A.

B = sort(A,dim) sorts A along dimension dim.

B = sort(A,dim,mode) sorts A in the order specified by mode. mode is 'ascend' for ascending order, or 'descend' for descending order.

[B,I] = sort(A,...) also returns an index matrix I. If A is a vector, then B = A(I). If A is an m-by-n matrix and dim is 1, then B(:,j) = A(I(:,j),j) for j = 1:n.

Elements with undefined levels are sorted to the end.

Examples

Sort the columns of an ordinal array in ascending order:

A = ordinal([6 2 5; 2 4 1; 3 2 4],...
            {'lo','med','hi'},[],[0 2 4 6])
A = 
     hi       med      hi 
     med      hi       lo 
     med      med      hi 

B = sort(A)
B = 
     med      med      lo 
     med      med      hi 
     hi       hi       hi

See Also

Was this topic helpful?