# Converting 1×n array to n-vector

**URL:** <https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723>\
**Category:** General Usage\
**Created:** [January 16, 2019, 8:29pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723 "2019-01-16T20:29:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [January 16, 2019, 8:29pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/1 "2019-01-16T20:29:19Z")

</div>

I would like to convert a 1×n array to an n-vector, e.g., `[1 2 3]` to `[1,2,3]`. What is the best way to do so?

My current method is first to convert it to a tuple and then to a vector:

```julia
julia> VERSION
v"0.7.0"

julia> w = [1 2 3]
1×3 Array{Int64,2}:
 1 2 3

julia> [(w...,)...]
3-element Array{Int64,1}:
 1
 2
 3

julia> @btime [($w...,)...]
  372.714 ns (2 allocations: 144 bytes)
3-element Array{Int64,1}:
 1
 2
 3

```

- Is there a more efficient way in terms of the number of allocations?
- Is there a more compact way syntactically?

---

<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:** [January 16, 2019, 8:33pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/2 "2019-01-16T20:33:25Z")

</div>

`vec([1 2 3])`

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 16, 2019, 8:41pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/3 "2019-01-16T20:41:38Z")

</div>

```nohighlight
julia> w = rand(1,10000000)
1×10000000 Array{Float64,2}:
 0.333815 0.882382 0.280182 0.85099 0.0466032 0.958258 … 0.486106 0.573983 0.494289 0.0294783 0.567801

julia> @time [(w...)...];
  1.661465 seconds (20.00 M allocations: 539.015 MiB, 53.93% gc time)

julia> @time vec(w);
  0.000003 seconds (6 allocations: 240 bytes)

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 16, 2019, 9:05pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/4 "2019-01-16T21:05:18Z")

</div>

Use `vec`, but be aware that the array and vector share the same memory:

```julia
julia> w = [1 2 3]
1×3 Array{Int64,2}:
 1 2 3

julia> v = vec(w)
3-element Array{Int64,1}:
 1
 2
 3

julia> v[1] = 42
42

julia> w
1×3 Array{Int64,2}:
 42 2 3

```

If you instead want a copy of the data, use `copy(vec(w))`.

---

<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:** [January 16, 2019, 11:24pm UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/5 "2019-01-16T23:24:59Z")

</div>

> [@bennedich](#):
>
> If you instead want a copy of the data, use `copy(vec(w))` .

In that case, I think `w[:]` would be convenient.

---

<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:** [January 17, 2019, 1:14am UTC](https://discourse.julialang.org/t/converting-1xn-array-to-n-vector/19723/6 "2019-01-17T01:14:24Z")

</div>

In fact in that case one would get automatically a one-dimensional array (vector).
