sorted_M = Matrices.sort(M); (sorted_M, indices) = Matrices.sort(M, sortRows=true, ascending=true);
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];
(M2, i2) := Matrices.sort([2, 1, 0; 2, 0, -1]); -> M2 = [2, 0, -1; 2, 1, 0 ]; i2 = {2,1};
function sort extends Modelica.Icons.Function; input Real M[:, :] "Matrix to be sorted"; input Boolean sortRows = true "= true if rows are sorted, otherwise columns"; input Boolean ascending = true "= true if ascending order, otherwise descending order"; output Real sorted_M[size(M, 1), size(M, 2)] = M "Sorted matrix"; output Integer indices[if sortRows then size(M, 1) else size(M, 2)] "sorted_M = if sortRows then M[indices,:] else M[:,indices]"; end sort;