Strange behaviour when copying into an element of a vector of matrices

Perhaps it would be clearer as follows:

julia> matrix = [1.;;]
1×1 Matrix{Float64}:
 1.0

julia> Zd = [matrix, matrix, matrix]
3-element Vector{Matrix{Float64}}:
 [1.0;;]
 [1.0;;]
 [1.0;;]

julia> matrix .= 2
1×1 Matrix{Float64}:
 2.0

julia> Zd  # Still Zd[i] === matrix for all i
3-element Vector{Matrix{Float64}}:
 [2.0;;]
 [2.0;;]
 [2.0;;]

In the example in my previous post, if I wanted the entries of Zd to be equal but not identical, I could have used Zd = [[1.;;] for _ = 1:3] (see also the documentation of fill).

3 Likes