# Difference between copy() and .= for two vectors

**URL:** <https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [September 20, 2021, 2:21am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429 "2021-09-20T02:21:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)\
**Post date:** [September 20, 2021, 2:21am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/1 "2021-09-20T02:21:01Z")

</div>

I need to copy the content of one vector to another that already exists, in this case is better to use `.= ` instead of the `copy()` function?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 20, 2021, 2:29am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/2 "2021-09-20T02:29:35Z")

</div>

> [@vinicius\_de\_lima](#):
>
> that already exists

if you use `copy()`, you’re effectively moving data twice:

```julia
a .= copy(b)

```

is the same as:

```julia
c = copy(b)
a .= c

```

so you should be better of just doing:

```julia
a .= b

```

of course, this is assuming you don’e need `deepcopy()`

---

<div class="post-metadata">

**Author:** ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)\
**Post date:** [September 20, 2021, 2:39am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/3 "2021-09-20T02:39:03Z")

</div>

Sorry, I think a didn’t express myself correctly, my intent was comparing  
`a = copy(b)`  
And  
`a .= b`

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 20, 2021, 4:11am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/4 "2021-09-20T04:11:20Z")

</div>

Perhaps you mean `copyto!`?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 20, 2021, 4:35am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/5 "2021-09-20T04:35:40Z")

</div>

There shouldn’t be any difference as such, as both the broadcast assignment and `copy` internally use `copyto!`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 20, 2021, 5:03am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/6 "2021-09-20T05:03:42Z")

</div>

> [@vinicius\_de\_lima](#):
>
> `a = copy(b)`  
> And  
> `a .= b`

Are you sure this is what you really meant? `a .= b` writes data into a pre-existing array, while `copy` must allocate a new one.

The more meaningful comparison would be `a .= b` and `copyto!(a, b)`

---

<div class="post-metadata">

**Author:** ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)\
**Post date:** [September 20, 2021, 11:41am UTC](https://discourse.julialang.org/t/difference-between-copy-and-for-two-vectors/68429/7 "2021-09-20T11:41:43Z")

</div>

Would be better just use `copyto!(a,b)` in this case then?
