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
| Type | Name | Default | Description |
|---|---|---|---|
| Real[:,:] | M | Matrix to be sorted | |
| Boolean | sortRows | true | = true, if rows are sorted, otherwise columns |
| Boolean | ascending | true | = true, if ascending order, otherwise descending order |
Outputs
| Type | Name | Default | Description |
|---|---|---|---|
| Real[size(M, 1),size(M, 2)] | sorted_M | M | Sorted matrix |
| Integer[if sortRows then size(M, 1) else size(M, 2)] | indices | sorted_M = if sortRows then M[indices,:] else M[:,indices] |
Contents
| Name | Description |
|---|---|
| Compare whether vector v1 > v2 | |
| Compare whether vector v1 < v2 |