# How to convert Vector of Vectors to Matrix

**URL:** <https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609>\
**Category:** General Usage\
**Tags:** question, arrays\
**Created:** [December 5, 2021, 1:35pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609 "2021-12-05T13:35:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![XLVII](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xlvii/32/37099_2.png) [@XLVII](https://discourse.julialang.org/u/XLVII)\
**Post date:** [December 5, 2021, 1:35pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/1 "2021-12-05T13:35:53Z")

</div>

I have a vector = [[1,0,1],[0,0,1]]

I am trying to convert this to → [1 0 1 ; 0 0 1]

How?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 5, 2021, 1:38pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/2 "2021-12-05T13:38:25Z")

</div>

What have you tried so far?

The answer is

```julia
julia> mapreduce(permutedims, vcat, x)
2×3 Matrix{Int64}:
 1 0 1
 0 0 1

```

---

<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:** [December 5, 2021, 4:03pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/3 "2021-12-05T16:03:46Z")

</div>

This does the job too:

```julia
permutedims(hcat(x...))

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 5, 2021, 7:12pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/4 "2021-12-05T19:12:27Z")

</div>

This could be a bit faster.

```julia
julia> reduce(vcat,transpose.(x))
2×3 Matrix{Int64}:
 1 0 1
 0 0 1

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 6, 2021, 3:07am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/5 "2021-12-06T03:07:46Z")

</div>

This is not desirable if `x` has many elements. But for the two-element array in the original example, it’s fine.

---

<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:** [December 6, 2021, 3:58am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/6 "2021-12-06T03:58:47Z")

</div>

For a large number of elements could do instead:

```julia
reduce(hcat,x)'

```

---

<div class="post-metadata">

**Author:** ![PaulN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauln/32/47997_2.png) [@PaulN](https://discourse.julialang.org/u/PaulN)\
**Post date:** [January 14, 2022, 7:17pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/7 "2022-01-14T19:17:09Z")

</div>

I noticed that I ran into a problem when there were more than 10,000 elements… Is there a workaround for this limitation? (Code would fail if I had 10,001 vector of vectors)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 14, 2022, 7:20pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/8 "2022-01-14T19:20:07Z")

</div>

Can you post an MWE? This is unexpected

---

<div class="post-metadata">

**Author:** ![PaulN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauln/32/47997_2.png) [@PaulN](https://discourse.julialang.org/u/PaulN)\
**Post date:** [January 14, 2022, 7:28pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/9 "2022-01-14T19:28:00Z")

</div>

I will; I am working on a notebook right now that once tidied up, I will send a link show a working example…  
-p

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 14, 2022, 7:29pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/10 "2022-01-14T19:29:10Z")

</div>

Please don’t send a link to a notebook. See if you can write the smallest amount of code that reproduces the problem and copy and paste it onto discourse.

---

<div class="post-metadata">

**Author:** ![PaulN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauln/32/47997_2.png) [@PaulN](https://discourse.julialang.org/u/PaulN)\
**Post date:** [January 14, 2022, 8:43pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/11 "2022-01-14T20:43:20Z")

</div>

will do

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 14, 2022, 11:54pm UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/12 "2022-01-14T23:54:07Z")

</div>

> [@PaulN](#):
>
> I noticed that I ran into a problem when there were more than 10,000 elements… Is there a workaround for this limitation? (Code would fail if I had 10,001 vector of vectors)

**Don’t use splatting** (`...`) of large arrays, e.g. _don’t_ use things like `hcat(x...)`. Use `reduce`, e.g. `reduce(hcat,x)` as suggested [above](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/6).

_Update:_ In Julia 1.9 or later, use the [`stack`](https://docs.julialang.org/en/v1/base/arrays/#Base.stack) function, e.g. `stack([[1,0,1],[0,0,1]], dims=1) == [1 0 1 ; 0 0 1]`.

---

<div class="post-metadata">

**Author:** ![guilhermebodin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guilhermebodin/32/6982_2.png) [@guilhermebodin](https://discourse.julialang.org/u/guilhermebodin)\
**Post date:** [January 15, 2022, 2:13am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/13 "2022-01-15T02:13:55Z")

</div>

I made this function in a package. Don’t know if it is better or worse than `reduce(hcat, x)`

```julia
function vector_of_vector_to_matrix(vov::Vector{Vector{T}}) where T
    n = length(vov)
    p = length(vov[1])
    mat = Matrix{T}(undef, n, p)
    for j in 1:p, i in 1:n
        mat[i, j] = vov[i][j]
    end
    return mat
end

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [January 15, 2022, 7:32am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/14 "2022-01-15T07:32:48Z")

</div>

There are already packages with such a function: see `combinedims()` in `SplitApplyCombine.jl`. It also has a `view`-based version that doesn’t copy data: `combinedimsview()`.

---

<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:** [January 15, 2022, 8:46am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/15 "2022-01-15T08:46:56Z")

</div>

> [@guilhermebodin](#):
>
> I made this function in a package. Don’t know if it is better or worse than `reduce(hcat, x)'`

Check it with `BenchmarkTools`.  
For, example:

```julia
vov = [rand(0:1,100) for _ in 1:100_000]
using BenchmarkTools
@btime vector_of_vector_to_matrix($vov) # 160 ms (2 allocations: 76 MiB)
@btime reduce(hcat,$vov)' # 17 ms (2 allocations: 76 MiB)

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 15, 2022, 9:42am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/16 "2022-01-15T09:42:37Z")

</div>

You don’t materialize the transpose in the latter case though. Should probably be included in the benchmark.

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [March 24, 2022, 10:00am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/17 "2022-03-24T10:00:25Z")

</div>

I run into the same problem by getting a vector of vector datatype with `[rand(x) for i=1:AM_SAMPLES]`, where rand(x) gives a vector. Afterward, I want a matrix out of it, but maybe I could avoid it beforehand. I am looking forward to your suggestions.

for me `hcat(x...)` did the job. I am wondering how `hcat(x...)` actually works

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [March 24, 2022, 10:02am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/18 "2022-03-24T10:02:39Z")

</div>

I am wondering how `hcat(x...)` actually works

---

<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:** [March 24, 2022, 11:05am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/19 "2022-03-24T11:05:23Z")

</div>

`hcat()` concatenates the arrays given as arguments along dimension 2. In your example when using the splatting operator `...` it does the same as writing explicitely the different element vectors of `x`:

```julia
x = [rand(10) for _ in 1:3] # 3-element Vector{Vector{Float64}}
hcat(x...) == hcat(x[1], x[2], x[3]) # true

```

As indicated, this is not the most efficient way of doing it when there are many elements to splat. The alternative: `reduce(hcat,x)` should be much better.

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [March 25, 2022, 9:19am UTC](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/20 "2022-03-25T09:19:05Z")

</div>

@rafael.guerra thank you for that example. I see that it works, but still, I am a bit confused about the splitting operator … Here is the example I have used:

```julia
julia> x=[1., 2., 3.]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> @show x...
ERROR: syntax: "..." expression outside call around ..
Stacktrace:
 [1] top-level scope
   @ none:1

julia> @show [x...]
[x...] = [1.0, 2.0, 3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> hcat(x...)
1×3 Matrix{Float64}:
 1.0 2.0 3.0

julia> hcat(x)
3×1 Matrix{Float64}:
 1.0
 2.0
 3.0

```

In this example, I see that I need brackets for the splatting operator. [x…] is still a 3 element vector in this example, but Julia does make a difference when the splatting operator is used. The splatting command allows julia to shred the vector in each element.

[Next page](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609.md?page=2)
