Copy vector?


#1

I just realized this behavior:

julia> x = [ 0. , 0. ]
2-element Array{Float64,1}:
 0.0
 0.0

julia> y = x
2-element Array{Float64,1}:
 0.0
 0.0

julia> y[1] = 1.
1.0

julia> x
2-element Array{Float64,1}:
 1.0
 0.0

x and y are just different names for the same vector. How do I copy a vector into another one, then?

Also, is there any style suggestion to avoid running into errors given that we could be modifying previous values of array variables this way?


#2

you can (should?) use copy

julia> x = [0., 0.];
julia> y = copy(x);
julia> y[1] = 1.;
julia> x
2-element Array{Float64,1}:
 0.0
 0.0

#3

Try copy or deepcopy, should do it. This is an intrinsic feature of the language; it is not a problem as long as one is aware of it.