你可以将我在另一个答案中发布的
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]
X
中,那么你可以直接使用CartesianIndices(X)
。这适用于任何维度和大小。 - DNFCartesianIndices(X)
和CartesianIndices(size(X))
的作用是相同的。如果X
不存在且您需要一个 NxNxNx... 的索引数组,则需要进行所有这些数字计算。 - BatWannaBe