MathNET.Numerics 中 Matlab sortrows() 的等效功能? [英] Equivalent functionality of Matlab sortrows() in MathNET.Numerics?

查看:40
本文介绍了MathNET.Numerics 中 Matlab sortrows() 的等效功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a MathNET.Numerics equivalent of Matlab’s sortrows(A, column), where A is a Matrix<double>?

回想 Matlab的文档:

B = sortrows(A,column)根据在向量列.例如,sortrows(A,4)对A中的行进行排序基于第四列中的元素的升序.sortrows(A,[4 6])首先根据中的元素对A的行进行排序第四列,然后根据第六列中的元素打破领带.

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.

推荐答案

类似于我对其他问题的回答,虽然没有内置内容,但是您可以使用 OrderBy() 方法矩阵行的>可枚举.给定 Matrix< double>x

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. Given a Matrix<double> x,

x.EnumerateRows()返回矩阵行的 Enumerable< Vector< double>> .然后,您可以按每行的第一个元素对此可枚举进行排序(如果需要的话).

x.EnumerateRows() returns an Enumerable<Vector<double>> of the matrix's rows. You can then sort this enumerable by the first element of each row (if that's what you want).

在C#中,

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

示例

将其写为扩展方法:

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

这是一个包含所有内容的小提琴

这篇关于MathNET.Numerics 中 Matlab sortrows() 的等效功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆