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
.
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]
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
Any information would be appreciated.
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.
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]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With