functionsort

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

Extends from Modelica.Icons.Function (Icon for functions).

Information

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};

Inputs

TypeNameDefaultDescription
Real[:,:]MMatrix to be sorted
BooleansortRowstrue= true, if rows are sorted, otherwise columns
Booleanascendingtrue= true, if ascending order, otherwise descending order

Outputs

TypeNameDefaultDescription
Real[size(M, 1),size(M, 2)]sorted_MMSorted matrix
Integer[if sortRows then size(M, 1) else size(M, 2)]indicessorted_M = if sortRows then M[indices,:] else M[:,indices]

Contents

NameDescription
greaterprotectedCompare whether vector v1 > v2
lessprotectedCompare whether vector v1 < v2