9

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.

1
  • There are some good answers below, of which 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.
    – DNF
    Dec 13, 2021 at 11:00

3 Answers 3

11

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.

1
  • Thank you. I didn't know CartesianIndices. I would like to use it when I face to this kind of situation.
    – ten
    Dec 13, 2021 at 11:17
10

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]
5

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.