# How to efficiently re-arrange a vector of vectors into a matrix?

**URL:** https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565
**Category:** New to Julia
**Tags:** array
**Created:** [October 28, 2021, 4:21pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565 "2021-10-28T16:21:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![erwanlecarpentier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erwanlecarpentier/32/23448_2.png) [@erwanlecarpentier](https://discourse.julialang.org/u/erwanlecarpentier)
#### Post date: [October 28, 2021, 4:21pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/1 "2021-10-28T16:21:36Z")

</div>

Hi! I would like to turn a vector of vectors into a matrix where each column corresponds to one of the input vectors. This could be accomplished with for instance the following:

```julia
julia> a = [[1, 2], [3, 4], [5, 6]]
3-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]
 [5, 6]

julia> reshape(collect(Iterators.flatten(a)), (length(a[1]),length(a)))
2×3 Matrix{Int64}:
 1 3 5
 2 4 6

```

Is it possible to do this operation more efficiently? I wonder if it is possible to create a `@view` object to avoid allocations.

```julia
julia> using BenchmarkTools

julia> @btime reshape(collect(Iterators.flatten($a)), (length($a[1]),length($a)))
  141.132 ns (5 allocations: 304 bytes)

```

---

<div class="post-metadata">

### Author: ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)
#### Post date: [October 28, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/2 "2021-10-28T16:28:21Z")

</div>

The way I have always seen it done is  
`hcat(a...)`  
Which brought the allocations from 7 to 2, for me, and sped it up by 2×.

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [October 28, 2021, 4:34pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/3 "2021-10-28T16:34:44Z")

</div>

`SplitApplyCombine.jl` has some utilities for this kind of thing, `combinedims`: [https://github.com/JuliaData/SplitApplyCombine.jl#combinedimsarray](https://github.com/JuliaData/SplitApplyCombine.jl#combinedimsarray) (and a lazy view version `combinedimsview` as well).

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 28, 2021, 4:46pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/4 "2021-10-28T16:46:20Z")

</div>

> [@cmarcotte](#):
>
> hcat(a…)

It’s generally faster to do `reduce(hcat, a)` since `reduce` has a special method for `hcat` and `vcat` which avoids some unnecessary allocations:

```julia
julia> a = [rand(2) for _ in 1:1000];

julia> using BenchmarkTools

julia> @btime reduce(hcat, $a);
  5.456 μs (1 allocation: 15.75 KiB)

julia> @btime hcat($a...);
  19.715 μs (6 allocations: 47.42 KiB)

```

If you actually have a bunch of small vectors with the same size, then it’s even faster (basically free) to use `reinterpret` with `StaticArrays`:

```julia
julia> using StaticArrays: SVector

julia> a = [rand(SVector{2, Float64}) for _ in 1:1000];

julia> @btime reshape(reinterpret(Float64, $a), (2, :));
  10.507 ns (0 allocations: 0 bytes)

```

That’s 10 nanoseconds and no allocation compared to 5 microseconds and 1 allocation for the `reduce(hcat, a)` version.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 28, 2021, 7:06pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/5 "2021-10-28T19:06:05Z")

</div>

For the record, using TensorCast nice syntax:

```julia
using TensorCast
@cast b[j,i] := v[i][j] # v is vector of vectors
@cast b[j,i] := sv[i]{j} # sv is vector of SVectors

```

---

<div class="post-metadata">

### Author: ![DiogoSantiago](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diogosantiago/32/30915_2.png) [@DiogoSantiago](https://discourse.julialang.org/u/DiogoSantiago)
#### Post date: [January 6, 2022, 5:14pm UTC](https://discourse.julialang.org/t/how-to-efficiently-re-arrange-a-vector-of-vectors-into-a-matrix/70565/6 "2022-01-06T17:14:12Z")

</div>

```julia
julia> a = [[1, 2], [3, 4], [5, 6]]
julia> reduce(hcat, a)
2×3 Matrix{Int64}:
 1 3 5
 2 4 6
julia> reduce(vcat, a')
3×2 Matrix{Int64}:
 1 2
 3 4
 5 6
julia> [a...;;]
2×3 Matrix{Int64}:
 1 3 5
 2 4 6
julia> [a'...;]
3×2 Matrix{Int64}:
 1 2
 3 4
 5 6

```

Many flavors, although even if i like Julia, i really think there’s a bag of tricks style in some points…
