MathNET.Numerics 中 Matlab sortrows() 的等效功能?

Equivalent functionality of Matlab sortrows() in MathNET.Numerics?

是否有 MathNET.Numerics 相当于 Matlab 的 sortrows(A, column),其中 A 是 Matrix<double>

召回Matlab's documentation:

B = sortrows(A,column) sorts A based on the columns specified in the vector column. For example, sortrows(A,4) sorts the rows of A in ascending order based on the elements in the fourth column. sortrows(A,[4 6]) first sorts the rows of A based on the elements in the fourth column, then based on the elements in the sixth column to break ties.

与我对您的其他问题的回答类似,没有任何内置内容,但您可以在矩阵行的 Enumerable 上使用 Linq's OrderBy() 方法。给定一个 Matrix<double> x,

x.EnumerateRows() returns 矩阵行的 Enumerable<Vector<double>>。然后,您可以按每行的第一个元素对这个可枚举对象进行排序(如果这是您想要的)。

在 C# 中,

var y = Matrix<double>.Build.DenseOfRows(x.EnumerateRows().OrderBy(row => row[0]));

Example

写成 extension method:

public static Matrix<double> SortRows(this Matrix<double> x, int sortByColumn = 0, bool desc = false) {
    if (desc)
        return Matrix<double>.Build.DenseOfRows(x.EnumerateRows().OrderByDescending(row => row[sortByColumn]));
    else
        return Matrix<double>.Build.DenseOfRows(x.EnumerateRows().OrderBy(row => row[sortByColumn]));
}

然后你可以这样调用:

var y = x.SortRows(0); // Sort by first column

Here's a big fiddle containing everything