How to convert Vector of Vectors to Matrix

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

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

How?

What have you tried so far?

The answer is

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

This does the job too:

permutedims(hcat(x...))
2 Likes

This could be a bit faster.

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

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

For a large number of elements could do instead:

reduce(hcat,x)'
4 Likes

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)

Can you post an MWE? This is unexpected

1 Like

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

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.

1 Like

will do

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.

1 Like

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

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

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().

Check it with BenchmarkTools.
For, example:

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)
1 Like

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