# Create a vector from n different vectors

**URL:** <https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 27, 2017, 11:53am UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336 "2017-02-27T11:53:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elia\_Lahouiri](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Elia\_Lahouiri](https://discourse.julialang.org/u/Elia_Lahouiri)\
**Post date:** [February 27, 2017, 11:53am UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/1 "2017-02-27T11:53:02Z")

</div>

How do i create a vector from n different vectors in a while loop?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2017, 11:59am UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/2 "2017-02-27T11:59:16Z")

</div>

Please explain more clearly what you want to do and show what you have already tried that didn’t work.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [February 27, 2017, 12:02pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/3 "2017-02-27T12:02:52Z")

</div>

You probably want append!

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [February 27, 2017, 3:26pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/4 "2017-02-27T15:26:04Z")

</div>

First of all, you should probably start by reading the documentation on [arrays](http://docs.julialang.org/en/stable/manual/arrays/). This will tell you mostly everything you need to know to work with them.

For your particular question, you want `vcat`, which concatenates arrays along the 1st dimension. If you have `n` vectors in an array of vectors `vectors`, you might do this:

```
new_vec = Vector{T}()
for vec in vectors
    new_vec = vcat(new_vec, vec)
end

```

Instead of `vcat(new_vec, vec)`, you can also write `[new_vec; vec]` as a convenient shorthand.

Edit: You could write this more concisely using a functional pattern (arguably more idiomatic):

```
new_vec = foldr(vcat, vectors)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2017, 7:11pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/5 "2017-02-27T19:11:26Z")

</div>

> `new_vec = vcat(new_vec, vec)`

This is a bad idea, since you are creating a new vector each time. As @cortner suggested, use `append!` instead.

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [February 27, 2017, 7:26pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/6 "2017-02-27T19:26:18Z")

</div>

Good point. I guess I’m a little too fond of one-line solutions.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 27, 2017, 7:33pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/7 "2017-02-27T19:33:49Z")

</div>

Actually I think you can use vcat but you need to splice, i.e.

```julia
new_vec = vcat(vectors...)

```

Quick benchmark:

```julia
julia> vectors = [rand(3) for i in 1:100];

julia> @benchmark vcat($vectors...)
BenchmarkTools.Trial: 
  memory estimate: 2.50 kb
  allocs estimate: 1
  --------------
  minimum time: 1.535 μs (0.00% GC)
  median time: 1.682 μs (0.00% GC)
  mean time: 2.388 μs (20.96% GC)
  maximum time: 585.015 μs (85.50% GC)
  --------------
  samples: 10000
  evals/sample: 10
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

versus:

```julia
julia> @benchmark foldr(vcat, $vectors)
BenchmarkTools.Trial: 
  memory estimate: 131.63 kb
  allocs estimate: 99
  --------------
  minimum time: 19.002 μs (0.00% GC)
  median time: 21.491 μs (0.00% GC)
  mean time: 42.924 μs (40.05% GC)
  maximum time: 3.319 ms (98.25% GC)
  --------------
  samples: 10000
  evals/sample: 1
  time tolerance: 5.00%
  memory tolerance: 1.00%

```

I imagine that the vcat implemented with many arguments preallocates the output and avoids creating N-1 intermediate arrays.

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [February 27, 2017, 8:45pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/8 "2017-02-27T20:45:15Z")

</div>

Cool, I thought there should be a clean way to write this so that the compiler could optimize it.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [February 27, 2017, 10:03pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/9 "2017-02-27T22:03:33Z")

</div>

I would like to take the opportunity to plug `Compat.Iterators.flatten`;

```julia
julia> using Compat.Iterators

julia> collect(flatten([rand(3) for i in 1:100]))
300-element Array{Float64,1}:
 0.320838   
 0.683946   
 0.119694   
 0.317296   
 0.686963   
 0.912702   
 0.602305   
 0.681549   
 0.814596   
 0.251851   
 ⋮          
 0.311022   
 0.000653191
 0.214496   
 0.489029   
 0.040118   
 0.848041   
 0.480453   
 0.384383   
 0.0319383  

```

`collect(flatten(vects))` is the way to do it in `Base`.

Note that this is equivalent to `[x for xs in xss for x in xs]` which effectively lowers to `collect(flatten(xss))`.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 27, 2017, 10:16pm UTC](https://discourse.julialang.org/t/create-a-vector-from-n-different-vectors/2336/10 "2017-02-27T22:16:32Z")

</div>

> Cool, I thought there should be a clean way to write this so that the compiler could optimize it.

Worth keeping in mind that what the compiler can optimize and what’s optimal are not necessarily the same thing. Splatting lots of arguments can sometimes introduce problems you wouldn’t have without splatting.
