Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a 2-dimensional lattice in Julia

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.

like image 409
ten Avatar asked Dec 13 '21 10:12

ten


3 Answers

It's not a Matrix of Vectors, but CartesianIndices serves this purpose.

L = zeros((5,5)) # example 5x5 Matrix

Li = CartesianIndices(size(L))
#=
5×5 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
 CartesianIndex(1, 1)  …  CartesianIndex(1, 5)
 CartesianIndex(2, 1)     CartesianIndex(2, 5)
 CartesianIndex(3, 1)     CartesianIndex(3, 5)
 CartesianIndex(4, 1)     CartesianIndex(4, 5)
 CartesianIndex(5, 1)     CartesianIndex(5, 5)
=#

If you must have the indices matrix like in your post, you could make a method that converts a CartesianIndex to a Vector and broadcast that method over the CartesianIndices:

CItoVector(CI) = collect(Tuple(CI))

CItoVector.(Li)
#=
5×5 Array{Array{Int64,1},2}:
 [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]
=#

But I recommend sticking with CartesianIndices because it doesn't allocate memory, and CartesianIndex is tailor-made for array indexing, which seems to be your intent.

like image 74
BatWannaBe Avatar answered Nov 03 '22 03:11

BatWannaBe


You can use an array comprehension:

julia> N = 5;

julia> L = [[i, j] for i in 1:N, j in 1:N]
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]
like image 43
fredrikekre Avatar answered Nov 03 '22 02:11

fredrikekre


What BatWannaBe recommends is the way I would do it, but just as a reference here is how you could get what you asked for using broadcasted vcat:

julia> vcat.(1:5, (1:5)')
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]
like image 24
Bogumił Kamiński Avatar answered Nov 03 '22 01:11

Bogumił Kamiński