I came across a behavior of arrays of arrays that I find unintuitive. I suspect I might be missing something basic here.
I think the following illustrates the behavior. Suppose I make a vector of matrices
julia> V = Vector{Matrix{Float64}}(undef, 2)
2-element Vector{Matrix{Float64}}:
#undef
#undef
and I would like to initialize each component of this vector to have size 2\times 2, say
julia> fill!(V, Matrix{Float64}(undef, 2, 2));
Then, I’d like to set the upper left element of the matrix in V[1]
to be 42.0
:
julia> V[1][1,1] = 42.0;
I find that it works, but it also sets the upper left element of the matrix in V[2]
to 42.0
julia> V[1]
2×2 Matrix{Float64}:
42.0 1.36587e-314
1.36587e-314 5.84085e-315
julia> V[2]
2×2 Matrix{Float64}:
42.0 1.36587e-314
1.36587e-314 5.84085e-315
Is there a way to assign values to the elements of these matrices independently? Is there an explanation for this behavior?