# Copy or view, issue

**URL:** <https://discourse.julialang.org/t/copy-or-view-issue/110395>\
**Category:** New to Julia\
**Tags:** matrices, copy, views\
**Created:** [February 19, 2024, 10:50am UTC](https://discourse.julialang.org/t/copy-or-view-issue/110395 "2024-02-19T10:50:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enlil50](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enlil50/32/206539_2.png) [@Enlil50](https://discourse.julialang.org/u/Enlil50)\
**Post date:** [February 19, 2024, 10:50am UTC](https://discourse.julialang.org/t/copy-or-view-issue/110395/1 "2024-02-19T10:50:36Z")

</div>

Following [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/) which says that:

> Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well. To avoid this, use A = copy(B).

I expected the following code to overwrite A. However this doesn’t happen:

```julia
using Random

function myfun()
    
    Random.seed!(1)
    A = Int8[0 1; 1 0]
    display(A)
    print("\n")

    block_A = A

    for i = 2:3
        display(block_A)
        block_A = rand(Int8, i,i)
    end
    display(block_A)
    print("\n")

    display(A)
end

myfun()

```

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [February 19, 2024, 10:56am UTC](https://discourse.julialang.org/t/copy-or-view-issue/110395/2 "2024-02-19T10:56:19Z")

</div>

What’s happening is that in the loop, the line:

```julia
block_A = rand(Int8, i, i)

```

is reassigning what `block_A` refers to, rather than altering it. So `A` is not affected. If you do something like:

```julia
for i = 2:2
        display(B)
        print("\n")
        B .= rand(Int8, i, i)
    end

```

where you use `.=` to reassign each element this has the expected effect.
