Recovering MATLAB addict needs help to understand Julia indexing

(In the REPL) Create a 1D array of 1D arrays of Points:

julia>using GLMakie
julia> A = fill(fill(Point2f0(0,0), 2), 2)
2-element Array{Array{Point{2,Float32},1},1}:
 [[0.0, 0.0], [0.0, 0.0]]
 [[0.0, 0.0], [0.0, 0.0]]

Change the value of the first point in the first vector of points:

julia> A[1][1] = Point2f0(1,1)
2-element Point{2,Float32} with indices SOneTo(2):
 1.0
 1.0

But …

julia> A
2-element Array{Array{Point{2,Float32},1},1}:
 [[1.0, 1.0], [0.0, 0.0]]
 [[1.0, 1.0], [0.0, 0.0]]

… why does this re-assign the first point in every vector of points? I expect the result of the previous snippet to be

 [[1.0, 1.0], [0.0, 0.0]]
 [[0.0, 0.0], [0.0, 0.0]]

what am I missing???

1 Like

fill puts the same object in every slot of the array. Instead, you could use an array comprehension to create a new object for each slot:

[[Point2f0(0,0) for _ in 1:2] for _ in 1:2]
7 Likes