What’s the most efficient way to convert a Matrix{T} of size 1*N or N*1 in Julia to a Vector{T}?

You can use the vec() function. It’s faster than the list comprehension and scales better with number of elements 😉
For a matrix of 1000×1:

julia> const a = reshape([1:1000],1000,1);

julia> typeof(a)
Array{Int64,2}

julia> vec_a = [x::Int for x in a];

julia> typeof(vec_a)
Array{Int64,1}

julia> vec_aII = vec(a);

julia> typeof(vec_aII)
Array{Int64,1}

6.41e-6 seconds # list comprehension

2.92e-7 seconds # vec()

Leave a Comment

tech