[英]Defining a multiple-dimensional array of arbitrary dimension in Julia
This question is related to this one .这个问题与这个有关。
In Julia, I wanted to make a 2-dimensional array of 5 x 5 with the (i, j) element having [i,j]
like this:在 Julia 中,我想制作一个 5 x 5 的二维数组,其中 (i, j) 元素具有
[i,j]
如下所示:
5×5 Matrix{Vector{Int64}}:
[1, 1] [1, 2] [1, 3] [1, 4] [1, 5]
[2, 1] [2, 2] [2, 3] [2, 4] [2, 5]
[3, 1] [3, 2] [3, 3] [3, 4] [3, 5]
[4, 1] [4, 2] [4, 3] [4, 4] [4, 5]
[5, 1] [5, 2] [5, 3] [5, 4] [5, 5]
I tried this with using array comprehension :我使用数组理解尝试了这个:
N = 5
L_2 = [[x1,x2] for x1 = 1:N, x2 = 1:N]
I want to generalize this definition for arbitrary dimension D
.我想将这个定义推广到任意维度
D
。
L_1 = [[x1] for x1 = 1:N] # 1-dimensional
L_2 = [[x1,x2] for x1 = 1:N, x2 = 1:N] # 2-dimensional
L_3 = [[x1,x2,x3] for x1 = 1:N, x2 = 1:N,x3 = 1:N] # 3-dimensional
...
#L_D = ??? # D-dimensional
How can I define?我该如何定义?
It is okay without using array comprehension.不使用数组理解也没关系。
Any information would be appreciated.任何信息,将不胜感激。
You can generalize the vcat
approach I have posted in the other answer like this:您可以概括我在其他答案中发布的
vcat
方法,如下所示:
julia> lattice(N, D) = vcat.((reshape(1:N, ntuple(j -> j == i ? N : 1, D)) for i in 1:D)...)
lattice (generic function with 1 method)
julia> lattice(2, 1)
2-element Vector{Vector{Int64}}:
[1]
[2]
julia> lattice(2, 2)
2×2 Matrix{Vector{Int64}}:
[1, 1] [1, 2]
[2, 1] [2, 2]
julia> lattice(2, 3)
2×2×2 Array{Vector{Int64}, 3}:
[:, :, 1] =
[1, 1, 1] [1, 2, 1]
[2, 1, 1] [2, 2, 1]
[:, :, 2] =
[1, 1, 2] [1, 2, 2]
[2, 1, 2] [2, 2, 2]
julia> lattice(2, 4)
2×2×2×2 Array{Vector{Int64}, 4}:
[:, :, 1, 1] =
[1, 1, 1, 1] [1, 2, 1, 1]
[2, 1, 1, 1] [2, 2, 1, 1]
[:, :, 2, 1] =
[1, 1, 2, 1] [1, 2, 2, 1]
[2, 1, 2, 1] [2, 2, 2, 1]
[:, :, 1, 2] =
[1, 1, 1, 2] [1, 2, 1, 2]
[2, 1, 1, 2] [2, 2, 1, 2]
[:, :, 2, 2] =
[1, 1, 2, 2] [1, 2, 2, 2]
[2, 1, 2, 2] [2, 2, 2, 2]
julia> lattice(2, 5)
2×2×2×2×2 Array{Vector{Int64}, 5}:
[:, :, 1, 1, 1] =
[1, 1, 1, 1, 1] [1, 2, 1, 1, 1]
[2, 1, 1, 1, 1] [2, 2, 1, 1, 1]
[:, :, 2, 1, 1] =
[1, 1, 2, 1, 1] [1, 2, 2, 1, 1]
[2, 1, 2, 1, 1] [2, 2, 2, 1, 1]
[:, :, 1, 2, 1] =
[1, 1, 1, 2, 1] [1, 2, 1, 2, 1]
[2, 1, 1, 2, 1] [2, 2, 1, 2, 1]
[:, :, 2, 2, 1] =
[1, 1, 2, 2, 1] [1, 2, 2, 2, 1]
[2, 1, 2, 2, 1] [2, 2, 2, 2, 1]
[:, :, 1, 1, 2] =
[1, 1, 1, 1, 2] [1, 2, 1, 1, 2]
[2, 1, 1, 1, 2] [2, 2, 1, 1, 2]
[:, :, 2, 1, 2] =
[1, 1, 2, 1, 2] [1, 2, 2, 1, 2]
[2, 1, 2, 1, 2] [2, 2, 2, 1, 2]
[:, :, 1, 2, 2] =
[1, 1, 1, 2, 2] [1, 2, 1, 2, 2]
[2, 1, 1, 2, 2] [2, 2, 1, 2, 2]
[:, :, 2, 2, 2] =
[1, 1, 2, 2, 2] [1, 2, 2, 2, 2]
[2, 1, 2, 2, 2] [2, 2, 2, 2, 2]julia> lattice(N, D) = vcat.([reshape(1:N, ntuple(j -> j == i ? N : 1, D)) for i in 1:D]...)
lattice (generic function with 1 method)
julia> lattice(2, 1)
2-element Vector{Vector{Int64}}:
[1]
[2]
julia> lattice(2, 2)
2×2 Matrix{Vector{Int64}}:
[1, 1] [1, 2]
[2, 1] [2, 2]
julia> lattice(2, 3)
2×2×2 Array{Vector{Int64}, 3}:
[:, :, 1] =
[1, 1, 1] [1, 2, 1]
[2, 1, 1] [2, 2, 1]
[:, :, 2] =
[1, 1, 2] [1, 2, 2]
[2, 1, 2] [2, 2, 2]
julia> lattice(2, 4)
2×2×2×2 Array{Vector{Int64}, 4}:
[:, :, 1, 1] =
[1, 1, 1, 1] [1, 2, 1, 1]
[2, 1, 1, 1] [2, 2, 1, 1]
[:, :, 2, 1] =
[1, 1, 2, 1] [1, 2, 2, 1]
[2, 1, 2, 1] [2, 2, 2, 1]
[:, :, 1, 2] =
[1, 1, 1, 2] [1, 2, 1, 2]
[2, 1, 1, 2] [2, 2, 1, 2]
[:, :, 2, 2] =
[1, 1, 2, 2] [1, 2, 2, 2]
[2, 1, 2, 2] [2, 2, 2, 2]
julia> lattice(2, 5)
2×2×2×2×2 Array{Vector{Int64}, 5}:
[:, :, 1, 1, 1] =
[1, 1, 1, 1, 1] [1, 2, 1, 1, 1]
[2, 1, 1, 1, 1] [2, 2, 1, 1, 1]
[:, :, 2, 1, 1] =
[1, 1, 2, 1, 1] [1, 2, 2, 1, 1]
[2, 1, 2, 1, 1] [2, 2, 2, 1, 1]
[:, :, 1, 2, 1] =
[1, 1, 1, 2, 1] [1, 2, 1, 2, 1]
[2, 1, 1, 2, 1] [2, 2, 1, 2, 1]
[:, :, 2, 2, 1] =
[1, 1, 2, 2, 1] [1, 2, 2, 2, 1]
[2, 1, 2, 2, 1] [2, 2, 2, 2, 1]
[:, :, 1, 1, 2] =
[1, 1, 1, 1, 2] [1, 2, 1, 1, 2]
[2, 1, 1, 1, 2] [2, 2, 1, 1, 2]
[:, :, 2, 1, 2] =
[1, 1, 2, 1, 2] [1, 2, 2, 1, 2]
[2, 1, 2, 1, 2] [2, 2, 2, 1, 2]
[:, :, 1, 2, 2] =
[1, 1, 1, 2, 2] [1, 2, 1, 2, 2]
[2, 1, 1, 2, 2] [2, 2, 1, 2, 2]
[:, :, 2, 2, 2] =
[1, 1, 2, 2, 2] [1, 2, 2, 2, 2]
[2, 1, 2, 2, 2] [2, 2, 2, 2, 2]
It doesn't seem like you want CartesianIndices for this, but for the record, CartesianIndices can take any Tuple of Int (more accurately Dims
aka NTuple{N,Int} where N
) to represent an Array's size.您似乎不需要 CartesianIndices ,但为了记录,CartesianIndices 可以采用任何 Int 元组(更准确地说
Dims
aka NTuple{N,Int} where N
)来表示数组的大小。 CartesianIndices((5,5))
for a 5x5, CartesianIndices((2,8,3))
for a 2x8x3, etc. You can make a quick NxNxNx... Tuple representing a size with D dimensions with NtotheD(N,D) = ntuple(i -> N, D)
. CartesianIndices((5,5))
用于 5x5, CartesianIndices((2,8,3))
用于 2x8x3 等。您可以快速创建 NxNxNx... 表示具有 D 维度的大小的元组NtotheD(N,D) = ntuple(i -> N, D)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.