# Assignment of array elements

**URL:** <https://discourse.julialang.org/t/assignment-of-array-elements/8738>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 1, 2018, 10:55am UTC](https://discourse.julialang.org/t/assignment-of-array-elements/8738 "2018-02-01T10:55:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvhulten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvhulten/32/2444_2.png) [@mvhulten](https://discourse.julialang.org/u/mvhulten)\
**Post date:** [February 1, 2018, 10:55am UTC](https://discourse.julialang.org/t/assignment-of-array-elements/8738/1 "2018-02-01T10:55:00Z")

</div>

I have an array `a` and I create an array `b` that has the same form and values as `a`. Then I want to change values in `b` alone, but they also change in `a`:

```julia
julia> a = ones(3)'
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> b = a
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> b[1] = 41
41

julia> a
1×3 RowVector{Float64,Array{Float64,1}}:
 41.0 1.0 1.0

```

I would expect this for functions, and not for the assignment operator `=`. Why does this specifically happen when changing individual values in an array?

When I assign a new value to the array as a whole, I guess `b` is reinitialised and hence the assignment doesn’t carry over to the original array `a` or something:

```julia
julia> b = zeros(3)'
1×3 RowVector{Float64,Array{Float64,1}}:
 0.0 0.0 0.0

julia> a
1×3 RowVector{Float64,Array{Float64,1}}:
 41.0 1.0 1.0

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 1, 2018, 11:49am UTC](https://discourse.julialang.org/t/assignment-of-array-elements/8738/2 "2018-02-01T11:49:43Z")

</div>

Check this out [Values vs. Bindings: The Map is Not the Territory · John Myles White](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/). In short, in the first case, `a` and `b` are both bound to the same data object (array in this case). When you rebind `b` to another data object, `a` is still bound to the first one. If you want them to be independent you will have to use `copy` for the above case, or `deepcopy` if your data object has a deeper structure and you want to copy the whole structure over.

Keywords: copying by value, copying by reference, variable binding, variable assignment, variable mutation, data mutation

---

<div class="post-metadata">

**Author:** ![mvhulten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvhulten/32/2444_2.png) [@mvhulten](https://discourse.julialang.org/u/mvhulten)\
**Post date:** [February 1, 2018, 12:32pm UTC](https://discourse.julialang.org/t/assignment-of-array-elements/8738/3 "2018-02-01T12:32:15Z")

</div>

Thank you, this works:

```julia
julia> a
1×3 RowVector{Float64,Array{Float64,1}}:
 41.0 1.0 1.0

julia> a = ones(3)'
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> b = copy(a)
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> b[1] = 41
41

julia> a
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

```

I need to build up my “Julia intuition”, such that I will be able to more easily see the link between the syntax and whether we talk about variable binding, assignment etc.
