F# - 对包含元组的矩阵进行排序
我找不到对以下元组矩阵的列中包含的值进行排序的方法:
Matrix<float * float> =
matrix [[(1.0, 145.0); (1.0, 45.0); (1.0, 130.0); (1.0, 30.0); (1.0, 130.0)]
[(2.0, 45.0); (2.0, 45.0); (2.0, 30.0); (2.0, 30.0); (2.0, 30.0)]
[(3.0, 130.0); (3.0, 30.0); (3.0, 145.0); (3.0, 45.0); (3.0, 130.0)]
[(4.0, 30.0); (4.0, 30.0); (4.0, 45.0); (4.0, 45.0); (4.0, 30.0)]
[(5.0, 130.0); (5.0, 30.0); (5.0, 130.0); (5.0, 30.0); (5.0, 145.0)]]
我想根据元组的第二个元素对每列进行排序。例如,这里的答案是:
matrix [[(1.0, 145.0); (1.0, 45.0); (3.0, 145.0); (3.0, 45.0); (5.0, 145.0)]
[(3.0, 130.0); (2.0, 45.0); (1.0, 130.0); (4.0, 45.0); (1.0, 130.0)]
[(5.0, 130.0); (3.0, 30.0); (5.0, 130.0); (1.0, 30.0); (3.0, 130.0)]
[(2.0, 45.0); (4.0, 30.0); (4.0, 45.0); (2.0, 30.0); (2.0, 30.0)]
[(4.0, 30.0); (5.0, 30.0); (2.0, 30.0); (5.0, 30.0); (4.0, 30.0)]]
提前谢谢您!
I do not find a way to sort the values included in the columns of the following matrix of tuples :
Matrix<float * float> =
matrix [[(1.0, 145.0); (1.0, 45.0); (1.0, 130.0); (1.0, 30.0); (1.0, 130.0)]
[(2.0, 45.0); (2.0, 45.0); (2.0, 30.0); (2.0, 30.0); (2.0, 30.0)]
[(3.0, 130.0); (3.0, 30.0); (3.0, 145.0); (3.0, 45.0); (3.0, 130.0)]
[(4.0, 30.0); (4.0, 30.0); (4.0, 45.0); (4.0, 45.0); (4.0, 30.0)]
[(5.0, 130.0); (5.0, 30.0); (5.0, 130.0); (5.0, 30.0); (5.0, 145.0)]]
I would like to sort each column depending on the second element of the tuple. For example here the answer would be :
matrix [[(1.0, 145.0); (1.0, 45.0); (3.0, 145.0); (3.0, 45.0); (5.0, 145.0)]
[(3.0, 130.0); (2.0, 45.0); (1.0, 130.0); (4.0, 45.0); (1.0, 130.0)]
[(5.0, 130.0); (3.0, 30.0); (5.0, 130.0); (1.0, 30.0); (3.0, 130.0)]
[(2.0, 45.0); (4.0, 30.0); (4.0, 45.0); (2.0, 30.0); (2.0, 30.0)]
[(4.0, 30.0); (5.0, 30.0); (2.0, 30.0); (5.0, 30.0); (4.0, 30.0)]]
Thank you in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,在使用数组(2D 和/或矩阵)时,我发现在内部使用数组通常是最快的方法。
例如,以可变方式组合 Daniel 和 Ankur 的方法:
我将矩阵转换为列数组('a[][],而不是 'a[,]),并对每列执行就地排序。之后,我用排序结果填充一个新矩阵。请注意,原始矩阵保持不变:列数组由列向量的副本填充(Vector.toArray 创建一个新数组)。
这种方法速度更快,因为它不需要转置、对列进行就地排序,并且不需要通过保持所有内容面向数组来与中间列表结构进行转换。我怀疑如果 Matrix 模块也支持与 'a[][] 之间的转换,那么速度可能会更快,尽管它可能并不真正适合矩阵。
另外,如果您不知道:您可以利用 F# 的元组结构比较按第二个元素降序、第一个元素升序排序:
示例:
In my experience, when working with arrays (2D and/or matrix) I found that working with arrays internally is often the fastest way to go.
For example, combining Daniel's and Ankur's approaches in a mutable way:
I converted the matrix to an array of columns ('a[][], not 'a[,]), and performed an in-place sort on each column. After that, I filled a new matrix with the sorted result. Note that the original matrix remains unmodified: the columns array is populated with copies of the column vectors (Vector.toArray creates a new array).
This approach is faster because it needs no transposes, sorts columns in place, and needs no conversion to and from intermediate list structures by keeping everything array-oriented. I suspect it could be made even faster if the Matrix module supported conversion to/from 'a[][] as well, although it's perhaps not really suited for matrices.
Also, in case you didn't know: you can make use of F#'s structural comparison of tuples to sort by second element descending, first element ascending:
Example:
下面是这样一个函数:
这个问题的具体用法:
更新: 使按 col 排序的函数变得通用。
Below is such a function:
Usage as particular to this question:
UPDATED: To make the sort by col function generic.
我以前从未使用过 Matrix,所以可能有更好的方法,但这似乎有效:
I've never used
Matrix
before so there might be a better way, but this seems to work: