Concatenate each

Given [[0,0,0],[1,1,1]] and [[4,5,6],[7,8,9],[10,11,12]], how can I get

[
  [[0,0,0],[4,5,6],[7,8,9],[10,11,12]], 
  [[1,1,1],[4,5,6],[7,8,9],[10,11,12]],
]

Not quite right:

julia> vcat.(([[0,0,0],[1,1,1]]), Ref([[4,5,6],[7,8,9],[10,11,12]]))
2-element Vector{Vector{Any}}:
 [0, 0, 0, [4, 5, 6], [7, 8, 9], [10, 11, 12]]
 [1, 1, 1, [4, 5, 6], [7, 8, 9], [10, 11, 12]]

Could be:

julia> a = [[0,0,0],[1,1,1]]
2-element Vector{Vector{Int64}}:
 [0, 0, 0]
 [1, 1, 1]

julia> b
3-element Vector{Vector{Int64}}:
 [4, 5, 6]
 [7, 8, 9]
 [10, 11, 12]

julia> [ [ai, b...] for ai in a]
2-element Vector{Vector{Vector{Int64}}}:
 [[0, 0, 0], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
 [[1, 1, 1], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

Trying with other methods I think you are looking for push! instead of vcat.

1 Like

A different but faster approach could be

begin
    cat_a = Vector{Vector{Vector{Int}}}(undef, length(a))
    for i in eachindex(a)
        cat_a[i] = vcat([a[i]], b)
    end
    cat_a
end

Your attempt can be fixed with an unexported function:

julia> vcat.(Base.vect.([[0,0,0],[1,1,1]]), Ref([[4,5,6],[7,8,9],[10,11,12]]))
2-element Vector{Vector{Vector{Int64}}}:
 [[0, 0, 0], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
 [[1, 1, 1], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
2 Likes

This looks like numpy array syntax, which is very different from Julia array syntax. Do you really mean for these to be vectors of vectors of vectors? Or are trying to create 3D arrays?

1 Like