Yeah, you're not going to do much better than that:
julia> function unlist{T}(vec_of_vecs::Vector{Vector{T}})
i = 0
for vec in vec_of_vecs
i += length(vec)
end
final = Array(T, i)
i = 1
for vec in vec_of_vecs
for element in vec
final[i] = element
i += 1
end
end
return final
end
unlist (generic function with 1 method)
julia> vec = Vector{Int}[[1,2,3,4,5], [6,7,8,9,10]]
2-element Array{Array{Int64,1},1}:
[1,2,3,4,5]
[6,7,8,9,10]
julia> unlist(vec)
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
julia> vec = Vector{Float64}[[1.0,2.0,3.0,4.0,5.0], [6.0,7.0,8.0,9.0,10.0]]
2-element Array{Array{Float64,1},1}:
[1.0,2.0,3.0,4.0,5.0]
[6.0,7.0,8.0,9.0,10.0]
julia> unlist(vec)
10-element Array{Float64,1}:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
On Mon, Oct 12, 2015 at 3:33 PM, Ben Ward <[email protected]> wrote:
> Hi,
>
> In R, if you have a list, containing two vectors that are - say - numeric,
> you can unlist them into one vector:
>
> > a <- list(c(1,2,3,4,5), c(7,8,9,10,11))
>
> > a
>
> [[1]]
>
> [1] 1 2 3 4 5
>
>
>
> [[2]]
>
> [1] 7 8 9 10 11
>
>
>
> > unlist(a)
>
> [1] 1 2 3 4 5 7 8 9 10 11
>
> >
>
>
> Is there a convenient way to do this with vectors in Julia? Say I have a
> Vector{Vector{Int}}:
>
> *vec = Vector{Int}[[1,2,3,4,5], [6,7,8,9,10]]*
>
> I can only think of creating a new vector and through some
> loopiness, filling it in.
>
> Thanks,
> Ben.
>