Hello everyone,
I’m encountering some difficulties when using the repeat
function and repeating arrays. My objective is creating an array of arrays, applying some function to it and getting the array back with its new value appended to it. In my case I’m trying to parallelyse the operations, but here I’m just showing the minimal working example of my problem.
julia> x_list = repeat([[0.25]], 3)
3-element Vector{Vector{Float64}}:
[0.25]
[0.25]
[0.25]
julia> r_list = [2.9, 3.0, 3.1]
3-element Vector{Float64}:
2.9
3.0
3.1
julia> for (x, r) in zip(x_list, r_list)
push!(x, x[1]+r)
end
Ideally my output would be
julia> x_list
3-element Vector{Vector{Float64}}:
[0.25, 3.15]
[0.25, 3.25]
[0.25, 3.35]
But thats not the case, instead I get
julia> x_list
3-element Vector{Vector{Float64}}:
[0.25, 3.15, 3.25, 3.35]
[0.25, 3.15, 3.25, 3.35]
[0.25, 3.15, 3.25, 3.35]
When I test x_list[1] === x_list[2]
and x_list[2] === x_list[3]
, I get true
in both cases. So when ˋrepeatˋ creates the array, in my limited understanding, it sets every item as a pointer to the same array stored in memory so they are indistinguishable and my code doesn’t work as expected.
My question is: is this expected behaviour? I’ve tested repeat
with integers and this doesn’t happen. Also, if it is expected, how can I solve my problem? In my specific implementation I still create an array with the initial condition, but the number of times I created is defined at runtime.