# Difference between \`A\[:\]\` and \`vec(A)\`

**URL:** <https://discourse.julialang.org/t/difference-between-a-and-vec-a/27507>\
**Category:** New to Julia\
**Created:** [August 13, 2019, 7:05pm UTC](https://discourse.julialang.org/t/difference-between-a-and-vec-a/27507 "2019-08-13T19:05:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [August 13, 2019, 7:05pm UTC](https://discourse.julialang.org/t/difference-between-a-and-vec-a/27507/1 "2019-08-13T19:05:25Z")

</div>

```julia-repl
julia> A, B = ones(3,), ones(3,);
julia> fn!(x) = begin x.=2 end;
julia> fn!(vec(A)); A
3×1 Array{Float64,2}:
 2.0
 2.0
 2.0
julia> fn!(B[:]); B
3×1 Array{Float64,2}:
 1.0
 1.0
 1.0

```

`vec`’s behaviour is understood from the [doc](https://docs.julialang.org/en/v1/base/arrays/#Base.vec).

I wonder if `[:]`'s behavior is expected, since the [_Performance Tips_](https://docs.julialang.org/en/v1/manual/performance-tips/#Access-arrays-in-memory-order,-along-columns-1):

> This can be verified using the `vec` function or the syntax `[:]` as shown below.

seems to suggest they should be indifferent.

Also, does this mean `vec` is more efficient than `[:]`?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 13, 2019, 7:44pm UTC](https://discourse.julialang.org/t/difference-between-a-and-vec-a/27507/2 "2019-08-13T19:44:03Z")

</div>

Yes, this is expected. Square-bracketed indexing creates copies.

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [August 14, 2019, 12:02am UTC](https://discourse.julialang.org/t/difference-between-a-and-vec-a/27507/3 "2019-08-14T00:02:22Z")

</div>

Note that if you use the `@views` macro, the result is what you were expecting (since there is no copy made):

```julia
julia> @views fn!(B[:]); B
3-element Array{Float64,1}:
 2.0
 2.0
 2.0

```
