Context
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:
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]
What I want to do
I want to generalize this definition for arbitrary dimension 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.
X
, then you can just doCartesianIndices(X)
. This works for any dimensionality and size. – ScarcityCartesianIndices(X)
does the same thing asCartesianIndices(size(X))
. All this number crunching is only needed ifX
doesn't already exist and you need a NxNxNx... array of indices. – Amphitheater