本文介绍了Julia:按第 2 列然后第 3 列对矩阵进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按第 2 列然后第 3 列对矩阵 A
进行排序.
I would like to sort my matrix A
by column 2 then 3.
A = round.(randn(100,4))
可能是这样的:
sort(A,(0,2:3))
100x4 Array{Float64,2}:
0.0 -2.0 -2.0 -1.0
-1.0 -2.0 -1.0 1.0
1.0 -2.0 -1.0 2.0
-1.0 -2.0 0.0 0.0
-1.0 -2.0 0.0 -1.0
-0.0 -2.0 0.0 -1.0
1.0 -2.0 0.0 0.0
1.0 -2.0 1.0 -1.0
-0.0 -2.0 2.0 -1.0
-0.0 -1.0 -2.0 1.0
⋮
-0.0 1.0 0.0 1.0
1.0 1.0 1.0 1.0
0.0 1.0 1.0 -1.0
-0.0 1.0 2.0 0.0
-0.0 2.0 -1.0 0.0
-2.0 2.0 -1.0 1.0
2.0 2.0 -0.0 -1.0
-1.0 2.0 -0.0 -1.0
1.0 2.0 0.0 2.0
-1.0 2.0 2.0 0.0
推荐答案
有一个 sortrows
函数采用 by
关键字,可以让你这样做:
There is a sortrows
function that takes a by
keyword that lets you do this:
julia> sortrows(A, by=x->(x[2],x[3]))
100x4 Array{Float64,2}:
2.0 -3.0 -0.0 0.0
-1.0 -2.0 -1.0 -1.0
-0.0 -2.0 -0.0 0.0
0.0 -2.0 0.0 -1.0
1.0 -2.0 1.0 2.0
-0.0 -2.0 1.0 -1.0
-1.0 -1.0 -2.0 1.0
-1.0 -1.0 -2.0 -0.0
-1.0 -1.0 -1.0 1.0
-0.0 -1.0 -1.0 0.0
⋮
-0.0 1.0 1.0 -1.0
-0.0 1.0 2.0 1.0
0.0 1.0 2.0 0.0
-1.0 2.0 -2.0 1.0
0.0 2.0 -2.0 -2.0
1.0 2.0 -1.0 0.0
0.0 2.0 -1.0 -0.0
-1.0 2.0 0.0 -1.0
-0.0 2.0 2.0 0.0
1.0 3.0 2.0 1.0
排序 API 非常灵活 - 您可以在此处找到文档.
The sorting API is pretty flexible – you can find documentation here.
这篇关于Julia:按第 2 列然后第 3 列对矩阵进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!