从Julia置换包中获取正则数组
我使用这个包来计算一长串输入的逆排列。以下代码几乎满足了我的要求:从Julia置换包中获取正则数组,julia,Julia,我使用这个包来计算一长串输入的逆排列。以下代码几乎满足了我的要求: using Permutations students = [[3 4 2 1] [3 4 1 2] [4 3 2 1]] students_inv = [Permutation(i)'.data' for i in eachrow(students)] 然后调用学生产生 3×4 Array{Int64,2}: 3 4 2 1 3 4 1 2 4 3 2 1 3-element Array{Lin
using Permutations
students = [[3 4 2 1]
[3 4 1 2]
[4 3 2 1]]
students_inv = [Permutation(i)'.data' for i in eachrow(students)]
然后调用学生
产生
3×4 Array{Int64,2}:
3 4 2 1
3 4 1 2
4 3 2 1
3-element Array{LinearAlgebra.Adjoint{Int64,Array{Int64,1}},1}:
[4 3 1 2]
[3 4 1 2]
[4 3 2 1]
调用students\u inv
3×4 Array{Int64,2}:
3 4 2 1
3 4 1 2
4 3 2 1
3-element Array{LinearAlgebra.Adjoint{Int64,Array{Int64,1}},1}:
[4 3 1 2]
[3 4 1 2]
[4 3 2 1]
我只想要一个3x4的整数数组,没有线性代数的东西。我怎样才能强制排列以获得更简单的输出?我想您正在寻找
mapsicles
,以及invperm
:
julia> students = [ 3 4 2 1
3 4 1 2
4 3 2 1 ];
julia> mapslices(invperm, students, dims=2)
3×4 Array{Int64,2}:
4 3 1 2
3 4 1 2
4 3 2 1
julia> using Permutations
julia> mapslices(row -> Permutation(row)'.data, students; dims=2)
3×4 Array{Int64,2}:
4 3 1 2
3 4 1 2
4 3 2 1
请注意,要输入文字矩阵,只需使用空格和换行符(或分号),而无需先分别构造行
还要注意,使用列而不是行可能更自然。不幸的是,在大型阵列上,mapsicles
的速度很慢。这样可以避免,或者您可能更喜欢全程使用向量向量(或元组向量),这取决于您正在做的其他事情
julia> reduce(hcat, map(invperm, eachcol(permutedims(students))))
4×3 Array{Int64,2}:
4 3 4
3 4 3
1 1 2
2 2 1
我想您正在寻找
mapsicles
,以及invperm
:
julia> students = [ 3 4 2 1
3 4 1 2
4 3 2 1 ];
julia> mapslices(invperm, students, dims=2)
3×4 Array{Int64,2}:
4 3 1 2
3 4 1 2
4 3 2 1
julia> using Permutations
julia> mapslices(row -> Permutation(row)'.data, students; dims=2)
3×4 Array{Int64,2}:
4 3 1 2
3 4 1 2
4 3 2 1
请注意,要输入文字矩阵,只需使用空格和换行符(或分号),而无需先分别构造行
还要注意,使用列而不是行可能更自然。不幸的是,在大型阵列上,mapsicles
的速度很慢。这样可以避免,或者您可能更喜欢全程使用向量向量(或元组向量),这取决于您正在做的其他事情
julia> reduce(hcat, map(invperm, eachcol(permutedims(students))))
4×3 Array{Int64,2}:
4 3 4
3 4 3
1 1 2
2 2 1
您可以使用
vcat
功能
julia> students_inv = vcat((Permutation(i)'.data' for i in eachrow(students))...)
3×4 Matrix{Int64}:
4 3 1 2
3 4 1 2
4 3 2 1
假设使用生成器()
符号代替列表理解[]
话虽如此,使用专栏版本更有效,因为Julia是专栏主修
julia> students = [[3, 4, 2, 1] [3, 4, 1, 2] [4, 3, 2, 1]]
4×3 Matrix{Int64}:
3 3 4
4 4 3
2 1 2
1 2 1
julia> students_inv = hcat((Permutation(i).data for i in eachcol(students))...)
4×3 Matrix{Int64}:
3 3 4
4 4 3
2 1 2
1 2 1
您可以使用
vcat
功能
julia> students_inv = vcat((Permutation(i)'.data' for i in eachrow(students))...)
3×4 Matrix{Int64}:
4 3 1 2
3 4 1 2
4 3 2 1
假设使用生成器()
符号代替列表理解[]
话虽如此,使用专栏版本更有效,因为Julia是专栏主修
julia> students = [[3, 4, 2, 1] [3, 4, 1, 2] [4, 3, 2, 1]]
4×3 Matrix{Int64}:
3 3 4
4 4 3
2 1 2
1 2 1
julia> students_inv = hcat((Permutation(i).data for i in eachcol(students))...)
4×3 Matrix{Int64}:
3 3 4
4 4 3
2 1 2
1 2 1
有一个内置的!明亮的谢谢,有一个内置的!明亮的非常感谢。