# Concatenate Vector of Vector with differents length

**URL:** <https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071>\
**Category:** New to Julia\
**Tags:** question, vector-of-vectors\
**Created:** [January 21, 2024, 6:13pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071 "2024-01-21T18:13:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blociss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blociss/32/16421_2.png) [@blociss](https://discourse.julialang.org/u/blociss)\
**Post date:** [January 21, 2024, 6:13pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/1 "2024-01-21T18:13:09Z")

</div>

Consider the following vector:

```julia
v = [
    (1, [[11, 11, 11], [12, 12, 12]]),
    (2, [[21, 21, 21]),
    (3, [[31, 31, 31], [32, 32, 32]]),
    (4, [[41, 41, 41]),
]

```

I want to get

```julia
4-element Vector{Any}:
[[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
[[11, 11, 11], [21, 21, 21], [32, 32, 32], [41, 41, 41]]
[[12, 12, 12], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
[[12, 12, 12], [21, 21, 21], [32, 32, 32], [41, 41, 41]]

```

When all row of the vector v have same length, there is a code from @rafael.guerra .

```julia
re = Vector{Vector{Int64}}[]
n = length(v[1][2])
for i in 1:n, k in 1:n
    res = Vector{Int64}[]
    push!(res, copy(v[1][2][k]))
    for j in 2:length(v)
        push!(res, copy(v[j][2][i]))
    end
    push!(re, res)
end

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 21, 2024, 7:04pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/2 "2024-01-21T19:04:57Z")

</div>

You Input `v` is this:

```julia
julia> v = [
           (1, [[11, 11, 11], [12, 12, 12]]),
           (2, [[21, 21, 21]]),
           (3, [[31, 31, 31], [32, 32, 32]]),
           (4, [[41, 41, 41]]),
       ]
4-element Vector{Tuple{Int64, Vector{Vector{Int64}}}}:
 (1, [[11, 11, 11], [12, 12, 12]])
 (2, [[21, 21, 21]])
 (3, [[31, 31, 31], [32, 32, 32]])
 (4, [[41, 41, 41]])

```

?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 21, 2024, 7:11pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/3 "2024-01-21T19:11:41Z")

</div>

```julia
julia> collect.(vec(collect(Iterators.product(last.(v)...))))
4-element Vector{Vector{Vector{Int64}}}:
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[11, 11, 11], [21, 21, 21], [32, 32, 32], [41, 41, 41]]
 [[12, 12, 12], [21, 21, 21], [32, 32, 32], [41, 41, 41]]

```

(trick is to use outer product of all four arrays in input - this isn’t in the exact order, but you mentioned it doesn’t matter)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 21, 2024, 8:40pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/4 "2024-01-21T20:40:34Z")

</div>

I like functional solutions better (like Dan’s).  
But if you like loops, here are some

```julia
res=[]
for i1 in eachindex(last.(v)[1])
  for i2 in eachindex(last.(v)[2])
    for i3 in eachindex(last.(v)[3])
      for i4 in eachindex(last.(v)[4])
        push!(res,getindex.(last.(v), [i1,i2,i3,i4]))
      end
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![blociss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blociss/32/16421_2.png) [@blociss](https://discourse.julialang.org/u/blociss)\
**Post date:** [January 21, 2024, 8:50pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/5 "2024-01-21T20:50:55Z")

</div>

yes, it is the right one

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 21, 2024, 9:06pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/6 "2024-01-21T21:06:51Z")

</div>

This is how I thought about it. The approach is similar to Dan’s.

```julia-repl
julia> v = [
           (1, [[11, 11, 11], [12, 12, 12]]),
           (2, [[21, 21, 21]]),
           (3, [[31, 31, 31], [32, 32, 32]]),
           (4, [[41, 41, 41]]),
       ]
4-element Vector{Tuple{Int64, Vector{Vector{Int64}}}}:
 (1, [[11, 11, 11], [12, 12, 12]])
 (2, [[21, 21, 21]])
 (3, [[31, 31, 31], [32, 32, 32]])
 (4, [[41, 41, 41]])

julia> vv = last.(v)
4-element Vector{Vector{Vector{Int64}}}:
 [[11, 11, 11], [12, 12, 12]]
 [[21, 21, 21]]
 [[31, 31, 31], [32, 32, 32]]
 [[41, 41, 41]]

julia> cis = CartesianIndices((length.(vv)...,))
CartesianIndices((2, 1, 2, 1))

julia> collect(cis)
2×1×2×1 Array{CartesianIndex{4}, 4}:
[:, :, 1, 1] =
 CartesianIndex(1, 1, 1, 1)
 CartesianIndex(2, 1, 1, 1)

[:, :, 2, 1] =
 CartesianIndex(1, 1, 2, 1)
 CartesianIndex(2, 1, 2, 1)

julia> map(cis[[1,3,2,4]]) do ci
           getindex.(last.(v), Tuple(ci))
       end
4-element Vector{Vector{Vector{Int64}}}:
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[11, 11, 11], [21, 21, 21], [32, 32, 32], [41, 41, 41]]
 [[12, 12, 12], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [21, 21, 21], [32, 32, 32], [41, 41, 41]]

```

---

<div class="post-metadata">

**Author:** ![blociss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blociss/32/16421_2.png) [@blociss](https://discourse.julialang.org/u/blociss)\
**Post date:** [January 22, 2024, 10:33am UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/7 "2024-01-22T10:33:06Z")

</div>

This works for me. Thanks

```julia
function cartesian_product(v)
    res = []
    g = collect(Iterators.product(last.(v)...))
    for gi in g
        push!(res, gi)
    end
    return res
end

```

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [January 22, 2024, 1:01pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/8 "2024-01-22T13:01:03Z")

</div>

A slightly shorter version of @Dan 's answer

```julia
Iterators.product(last.(v)...) .|> collect |> vec

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 22, 2024, 5:37pm UTC](https://discourse.julialang.org/t/concatenate-vector-of-vector-with-differents-length/109071/9 "2024-01-22T17:37:01Z")

</div>

this is not correct

```julia
eachcol.(last(accumulate((s,c)->hcat.(s,c), lv[2:end], init=permutedims(lv[1]))))[:]

```

this should be better

```julia
eachcol.(reduce((s,c)->hcat.(reshape(s,1,:),c), last.(v)))[:]

```
