Consider the following bit of code:
using Random
# starting point
X₀ = [-1.0,0.0];
X_vals = [X₀]
X = similar(X₀)
n = 5;
Random.seed!(100);
@. X = X₀;
for j in 1:n
@. X = X + randn();
println(j, ": X[1] = ", X[1], "X[2] = ", X[2])
push!(X_vals, X)
end
display(X_vals)
This outputs:
1: X[1] = -1.6581360512008891X[2] = 0.3837531807164861
2: X[1] = -2.2595570449176217X[2] = 0.17223651755470998
3: X[1] = -0.6808178044351569X[2] = 2.20480043688056
4: X[1] = -0.2847472811211521X[2] = 1.3340975860113324
5: X[1] = -0.9904282830550172X[2] = 2.9718085271757393
6-element Array{Array{Float64,1},1}:
[-1.0, 0.0]
[-0.990428, 2.97181]
[-0.990428, 2.97181]
[-0.990428, 2.97181]
[-0.990428, 2.97181]
[-0.990428, 2.97181]
As you can see, it seems to only be storing a reference to the X
variable, instead of its value. If I instead use push!(X_vals, copy(X))
, I get the expected results.
Is this the expected functionality?