# Difference between element-wise allocation and copy()

**URL:** https://discourse.julialang.org/t/difference-between-element-wise-allocation-and-copy/111803
**Category:** New to Julia
**Created:** [March 19, 2024, 7:36am UTC](https://discourse.julialang.org/t/difference-between-element-wise-allocation-and-copy/111803 "2024-03-19T07:36:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TongTongYee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tongtongyee/32/207244_2.png) [@TongTongYee](https://discourse.julialang.org/u/TongTongYee)
#### Post date: [March 19, 2024, 7:36am UTC](https://discourse.julialang.org/t/difference-between-element-wise-allocation-and-copy/111803/1 "2024-03-19T07:36:19Z")

</div>

Hi!

I was using the elemet-wize allocation ( .= ) as the copy function. But I found that in certain cases, it’s not. I want to know in which case I can use element-wize allocation as the copy function.

for example

```julia
C11 = fill(0.0im,(2,10))
C12 = fill(0.0im,(2,10))
for i = 1: 2
    C13 = fill(i,10)
    C11[i,:,:] = copy(C13)
    C12[i,:,:] .= 1*C13
end
C12[1,:] # ten of 1.0
C12[2,:] # ten of 2.0
C11[1,:] # ten of 1.0
C12[2,:] # ten of 2.0

C15 = fill(1.0im*fill(0.0,(10)),(2))
C14 = fill(1.0im*fill(0.0,(10)),(2))
for i = 1: 2
    C13 = fill(i,10)
    C14[i] = copy(C13)
    C15[i] .= 1*C13
end
C14[1] # ten of 1.0
C14[2] # ten of 2.0
**C15[1] # ten of 2.0**
C15[2] # ten of 2.0

```

I found strange results

```julia
C14 = fill(1.0im*fill(0.0,(10)),(2))
for i = 1: 2
    C13 = fill(i,10)
    C15[i] **.=** C13
end
C15[1] # 2.0
for i = 1: 2
    C13 = fill(i,10)
    C15[i] **=** C13
end
C15[1] #1.0
for i = 1: 2
    C13 = fill(i,10)
    C15[i] **.=** C13
end
C15[1] # 1.0

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 19, 2024, 12:18pm UTC](https://discourse.julialang.org/t/difference-between-element-wise-allocation-and-copy/111803/3 "2024-03-19T12:18:32Z")

</div>

> [@TongTongYee](#):
>
> `C15 = fill(1.0im*fill(0.0,(10)),(2))`

You are having a [`fill` array-of-arrays problem](https://stackoverflow.com/questions/41072991/why-do-these-two-different-ways-of-constructing-an-array-produce-different-behav). Hint: do `C15[1][1] = 1` and then look at `C15[2]`.

TDLR: You are initializing your arrays so that every element “points” to the _same_ array, so that mutating one element’s array mutates them all.

> [@TongTongYee](#):
>
> ```julia
> C14[i] = copy(C13)
> C15[i] .= 1*C13
> 
> ```

This is compounding your `fill` confusion with [assignment vs. mutation](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions) confusion.

TLDR: `= copy` assigns that element to a new array, without affecting the other elements, whereas `.=` mutates the existing array (which affects all your other elements due to the `fill` confusion).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 19, 2024, 12:28pm UTC](https://discourse.julialang.org/t/difference-between-element-wise-allocation-and-copy/111803/4 "2024-03-19T12:28:14Z")

</div>

> [@stevengj](#):
>
> You are having a [`fill` array-of-arrays problem](https://stackoverflow.com/questions/41072991/why-do-these-two-different-ways-of-constructing-an-array-produce-different-behav).

I’ve often wanted a package that could take code like this:

```julia
a = fill([3,1,4], 4)

```

and draw a diagram like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/0/50d6c3160872e1668a4a2ee455e94a8a76318e39.png)

as opposed to

```julia
b = [[3,1,4] for i=1:4]

```

for which it would draw:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a53a8dac8e4f6e5c29032b582c4a89ed38bd5d9.png)
