# Problem with mutation and copying of Matrices/Vectors

**URL:** <https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221>\
**Category:** General Usage\
**Tags:** question, arrays, mutable-structure\
**Created:** [December 16, 2021, 6:56pm UTC](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221 "2021-12-16T18:56:11Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [December 20, 2021, 10:10pm UTC](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221/21 "2021-12-20T22:10:34Z")

</div>

@stevengj  
Thank you very much for your explanations which are very helpful.  
It seems that I am confused between the terms “Replace”, “overwrite”, “change”. I am also confused between:

```julia
R.I .= @SVector [3,4,5] # .= changes the *contents* of R.I
R.I = @SVector [3,4,5] # = does not work as (changing the *contents* of R.I also?)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [December 20, 2021, 10:16pm UTC](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221/22 "2021-12-20T22:16:46Z")

</div>

> [@Henrique\_Becker](#):
>
> if you create an immutable `struct` with an `Vector` inside, then you cannot replace the `Vector` object in the field by another `Vector` object that you have allocated after, **but** the stored `Vector` itself can be changed

It seems that I am confused between the two terms “Replace”, and “change”. Could you please explain the difference to me?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 21, 2021, 12:23am UTC](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221/23 "2021-12-21T00:23:34Z")

</div>

```julia
julia> struct IntVectorWrapper; v :: Vector{Int}; end

julia> x = IntVectorWrapper(Int[1, 2, 3]);

julia> x.v = [4, 5, 6]
ERROR: setfield! immutable struct of type IntVectorWrapper cannot be changed
Stacktrace:
 [1] setproperty!(::IntVectorWrapper, ::Symbol, ::Array{Int64,1}) at ./Base.jl:34
 [2] top-level scope at REPL[3]:1

julia> empty!(x.v); append!(x.v, [4, 5, 6]); x.v
3-element Array{Int64,1}:
 4
 5
 6

```

It is not about the terms, it is about what you are changing/replacing/mutating. You cannot change/replace/mutate `x` directly because it is immutable. However, the vector object stored in `x.v` is a mutable object (because vectors are mutable), so you can change it. You just cannot ever change `x`, so `x` will always point to the same mutable object in `x.v` and you must change the stored object itself, and not which object is stored there. Is it clearer?

[Previous page](https://discourse.julialang.org/t/problem-with-mutation-and-copying-of-matrices-vectors/73221.md?page=1)
