sort

Sort the rows or columns of a matrix in ascending or descending order

Information

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Syntax

           sorted_M = Matrices.sort(M);
(sorted_M, indices) = Matrices.sort(M, sortRows=true, ascending=true);

Description

Function sort(..) sorts the rows of a Real matrix M in ascending order and returns the result in sorted_M. If the optional argument "sortRows" is false, the columns of the matrix are sorted. If the optional argument "ascending" is false, the rows or columns are sorted in descending order. In the optional second output argument, the indices of the sorted rows or columns with respect to the original matrix are given, such that

sorted_M = if sortedRow then M[indices,:] else M[:,indices];

Example

(M2, i2) := Matrices.sort([2, 1,  0;
                           2, 0, -1]);
     -> M2 = [2, 0, -1;
              2, 1, 0 ];
        i2 = {2,1};

Syntax

(sorted_M, indices) = sort(M, sortRows, ascending)

Inputs (3)

M

Type: Real[:,:]

Description: Matrix to be sorted

sortRows

Default Value: true

Type: Boolean

Description: = true, if rows are sorted, otherwise columns

ascending

Default Value: true

Type: Boolean

Description: = true, if ascending order, otherwise descending order

Outputs (2)

sorted_M

Default Value: M

Type: Real[size(M, 1),size(M, 2)]

Description: Sorted matrix

indices

Type: Integer[if sortRows then size(M, 1) else size(M, 2)]

Description: sorted_M = if sortRows then M[indices,:] else M[:,indices]