Main Content

sort

Sort elements of real-valued fi object in ascending or descending order

Description

example

B = sort(A) sorts the elements of the real-valued fi object A in ascending order.

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

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

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

example

B = sort(A,dim) returns the sorted elements of A along dimension dim.

example

B = sort(___,direction) returns sorted elements of A in the order specified by direction.

example

[B,I] = sort(___) also returns a collection of index vectors for any of the previous syntaxes.

Examples

collapse all

Create a fi row vector and sort its elements in ascending order.

A = fi([9 0 -7 5 3 8 -10 4 2]);
B = sort(A)
B = 

   -10    -7     0     2     3     4     5     8     9

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 11

Create a matrix of fi values and sort its columns in descending order.

A = fi([10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]);
B = sort(A,'descend')
B = 

    10     3    11     8
     6     1     9     3
     2    -9     8     0
     1   -12     4    -2

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 11

Create a matrix of fi values and sort each of its rows in ascending order.

A = fi([3 6 5; 7 -2 4; 1 0 -9]);
[B,I] = sort(A,2)
B = 

     3     5     6
    -2     4     7
    -9     0     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 11

I =

  3×3 int32 matrix

   1   3   2
   2   3   1
   3   2   1

B contains the sorted values and I is a collection of 1-by-3 row index vectors describing the rearrangement of each row of A.

Input Arguments

collapse all

Input array, specified as a real-valued fi object.

  • If A is a scalar, then sort(A) returns A.

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

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

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

sort does not support complex fixed-point inputs, or pairs of Name,Value arguments. Refer to the MATLAB® sort reference page for more information.

Data Types: fi

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

The dimensions argument must be a built-in data type; it cannot be a fi object.

Example: Consider a matrix A. sort(A,1) sorts the elements in the columns of A.

Example: sort(A,2) sorts the elements in the rows of A.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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

Data Types: char

Output Arguments

collapse all

Sorted array, returned as a scalar, vector, matrix, or multidimensional array. B is the same size and type as A. The order of the elements in B preserves the order of any equal elements in A.

Sort index, returned as a scalar, vector, matrix, or multidimensional array. I is the same size as A. The index vectors are oriented along the same dimension that sort operates on.

Example: If A is a vector, then B = A(I).

Example: If A is a 2-by-3 matrix, then [B,I] = sort(A,2) sorts the elements in each row of A. The output I is a collection of 1-by-3 row index vectors describing the rearrangement of each row of A.

Extended Capabilities

Introduced in R2008b