[英]Equivalent functionality of Matlab unique() in MathNET.Numerics?
Is there a MathNET.Numerics equivalent of Matlab's unique(A, 'rows')
(or unique(A)
), where A
is a Matrix<double>
?是否有 MathNET.Numerics 等效于 Matlab 的unique(A, 'rows')
(或unique(A)
),其中A
是Matrix<double>
?
I have searched extensively through the MathNET.Numerics library documentation, and cannot find anything resembling this functionality.我已经通过 MathNET.Numerics 库文档进行了广泛的搜索,但找不到与此功能类似的任何内容。 Does similar functionality already exist?是否已经存在类似的功能?
To recall Matlab's documentation:回忆一下 Matlab 的文档:
C = unique(A,'rows') treats each row of A as a single entity and returns the unique rows of A. The rows of the array C are in sorted order. C = unique(A,'rows') 将 A 的每一行视为单个实体并返回 A 的唯一行。数组 C 的行按排序顺序排列。
There's nothing inbuilt, but you could use Linq
's Distinct()
method on an Enumerable
of the matrix's rows.没有内置任何内容,但您可以在矩阵行的Enumerable
上使用Linq
的Distinct()
方法。 Given a Matrix<double> x
,给定一个Matrix<double> x
,
var y = Matrix<double>.Build.DenseOfRows(x.EnumerateRows().Distinct());
Writing this as an extension method :将此作为扩展方法编写:
public static Matrix<double> Unique(this Matrix<double> x) {
return Matrix<double>.Build.DenseOfRows(x.EnumerateRows().Distinct());
}
Which you can then call as:然后您可以将其称为:
var y = x.Unique();
This doesn't sort the rows.这不会对行进行排序。 If you want that, you could combine this with this answer .如果你想要那个,你可以把它和这个答案结合起来。
public static Matrix<double> UniqueSorted(this Matrix<double> x, int sortByColumn = 0, bool desc = false) {
var uq = x.EnumerateRows().Distinct();
if (desc)
return Matrix<double>.Build.DenseOfRows(uq.OrderByDescending(row => row[sortByColumn]));
else
return Matrix<double>.Build.DenseOfRows(uq.OrderBy(row => row[sortByColumn]));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.