# How Julia loop works with vector?

**URL:** <https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 22, 2017, 9:00pm UTC](https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616 "2017-10-22T21:00:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MonkieDein](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MonkieDein](https://discourse.julialang.org/u/MonkieDein)\
**Post date:** [October 22, 2017, 9:00pm UTC](https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616/1 "2017-10-22T21:00:46Z")

</div>

![Julia vs Matlab](https://global.discourse-cdn.com/julialang/original/3X/f/7/f7597b9297d5eb5f9d22cf26b8b41c089ab36faf.png)  
Recently I have been encountering a problem that I want to make a vector that store the old matrix that I need to make my new matrix. However, in Julia while I change my new matrix, the old matrix follow. So, I wonder how should I do this step in Julia?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2017, 9:13pm UTC](https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616/2 "2017-10-22T21:13:30Z")

</div>

You should `copy` the vector if you need, well, a copy. There’s no automatic copy on assigment and all assignments has the semantic of binding to the reference of an object instead of copying.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 22, 2017, 9:41pm UTC](https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616/3 "2017-10-22T21:41:04Z")

</div>

`y2 = copy(y)`, otherwise you set them to reference the same memory.

---

<div class="post-metadata">

**Author:** ![MonkieDein](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@MonkieDein](https://discourse.julialang.org/u/MonkieDein)\
**Post date:** [October 23, 2017, 2:54am UTC](https://discourse.julialang.org/t/how-julia-loop-works-with-vector/6616/4 "2017-10-23T02:54:39Z")

</div>

Thank you guys, that is very helpful
