在 Julia 1.9 中,您需要使用
eachslice
:
julia> x = rand(Int8,3,3,2,2)
3×3×2×2 Array{Int8, 4}:
[:, :, 1, 1] =
127 107 27
31 -90 95
-50 -92 117
...
julia> eachslice(x; dims=(3,4))
2×2 Slices{Array{Int8, 4}, Tuple{Colon, Colon, Int64, Int64}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Int8, 2, Array{Int8, 4}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, Int64, Int64}, true}, 2}:
[127 107 27; 31 -90 95; -50 -92 117] [-58 7 -16; -77 -110 32; -82 25 -48]
[-40 13 -43; -63 121 47; 28 -64 57] [62 -61 19; 117 42 101; 70 -82 -62]
julia> eachslice(x; dims=(1,2))
3×3 Slices{Array{Int8, 4}, Tuple{Int64, Int64, Colon, Colon}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Int8, 2, Array{Int8, 4}, Tuple{Int64, Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}, 2}:
[127 -58; -40 62] [107 7; 13 -61] [27 -16; -43 19]
[31 -77; -63 117] [-90 -110; 121 42] [95 32; 47 101]
[-50 -82; 28 70] [-92 25; -64 -82] [117 -48; 57 -62]
如上所述,通常最好使用包含前导维度的切片,例如
eachslice(x; dims=(3,4))
产生的
x[:,:,i,j]
。
在Julia 1.8及以下版本中,理想情况下,
Compat.jl#663将使此功能正常工作。
A[:,i,j,k]
的元素在内存中紧密排列。因此,将A
转换为一个nKxnK
元素的mxm
矩阵是有意义的。 - Dan Getz