简体   繁体   English

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

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

Is there a MathNET.Numerics equivalent of Matlab's sortrows(A, column) , where A is a Matrix<double> ?是否有 MathNET.Numerics 等价于 Matlab 的sortrows(A, column) ,其中 A 是Matrix<double>

To recall Matlab's documentation :回忆一下Matlab 的文档

B = sortrows(A,column) sorts A based on the columns specified in the vector column. B = sortrows(A,column) 根据向量列中指定的列对 A 进行排序。 For example, sortrows(A,4) sorts the rows of A in ascending order based on the elements in the fourth column.例如, sortrows(A,4) 根据第四列中的元素按升序对 A 的行进行排序。 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. sortrows(A,[4 6]) 首先根据第四列中的元素对 A 的行进行排序,然后根据第六列中的元素进行排序以打破平局。

Similar to my answer to your other question, there's nothing inbuilt but you could use Linq 's OrderBy() method on an Enumerable of the matrix's rows.与我对您的其他问题的回答类似,没有内置任何内容,但您可以在矩阵行的Enumerable上使用LinqOrderBy()方法。 Given a Matrix<double> x ,给定一个Matrix<double> x

x.EnumerateRows() returns an Enumerable<Vector<double>> of the matrix's rows. x.EnumerateRows()返回矩阵行的Enumerable<Vector<double>> You can then sort this enumerable by the first element of each row (if that's what you want).然后,您可以按每行的第一个元素对此可枚举项进行排序(如果这是您想要的)。

In C#,在 C# 中,

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

Example例子

Writing this as an 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]));
}

which you can then call like so:然后你可以这样调用:

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

Here's a big fiddle containing everything这是一个包含所有东西的大小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM