Context
Let us consider 5 x 5 lattice with each point indexed as (1,1),(1,2),...(1,5),(2,1),...,(5,5), and call this lattice L
.
What I want to do
I want to make a 5 x 5 matrix with each element having a value which indicates each point of L
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]
What I tried
I just tried the following:
X1 = [1,2,3,4,5]
X2 = copy(X1)
Lattice = Matrix{Vector{Int64}}(undef, length(X1), length(X2)) # what I want to make
for x1 in X1
for x2 in X2
Lattice[x1,x2] = [X1[x1],X2[x2]]
end
end
Lattice
Question
- Is there other ways to make the code simple or short?
- I'm afraid if the performance get worse when increasing the lattice size like 50 x 50. Any better way?
- Whatever better practice?
Any information would be appreciated.
CartesianIndices
is the fastest and most lightweight (it does not use more memory, no matter how big your lattice becomes.) But if you want answers targeted at your usecase, you should provide more details on what you want to use this for.