# Vectors of vectors : Concatenation

**URL:** <https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061>\
**Category:** New to Julia\
**Tags:** question, vector-of-vectors\
**Created:** [January 21, 2024, 12:38am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061 "2024-01-21T00:38:19Z")\
**Posts on this page:** 7\
**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, 12:38am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/1 "2024-01-21T00:38:19Z")

</div>

Consider this vector

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

```

I want to get from v the following vector _vv_

```julia

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

```

I tried

```julia
re = []

for i in 1:length(v[1][2])
    res = []
    for j in 1:length(v)
        push!(res, copy(v[j][2][i]))
    end
    push!(re, res)
end

```

which give me only two vectors

```julia
[[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
[[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 21, 2024, 12:49am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/2 "2024-01-21T00:49:16Z")

</div>

This is close:

```julia
using TensorCast
@cast w[j][i] |= repeat.(last.(v), 2)[i][j]

4-element Vector{Vector{Vector{Int64}}}:
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]

```

---

<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, 12:52am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/3 "2024-01-21T00:52:21Z")

</div>

Can you please do it using for loop?

---

<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, 1:38am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/4 "2024-01-21T01:38:30Z")

</div>

Note that, the OP writes:

> [@blociss](#):
>
> ```julia
> [[11, 11, 11], 
> [[11, 11, 11],
> 
> ```

while the last answer has:

> [@rafael.guerra](#):
>
> ```julia
> [[11, 11, 11], 
> [[12, 12, 12], 
> 
> ```

which are different. rafael’s answer looks more sensible, but is it a typo in the original post?

Also, you can try:

```julia
julia> collect.(repeat(zip(last.(v)...)|> collect,2))
4-element Vector{Vector{Vector{Int64}}}:
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]
 [[11, 11, 11], [21, 21, 21], [31, 31, 31], [41, 41, 41]]
 [[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]

```

Note there are a lot of `collect` functions in this expression which is often a sign something is not calculated right (as `collect`s are usually a spurious waste of allocation/time).

---

<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, 10:31am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/7 "2024-01-21T10:31:06Z")

</div>

What you get is

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

```

What I want is

```julia
[[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], [22, 22, 22], [32, 32, 32], [42, 42, 42]]
[[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]

```

The order doesn’t matter .

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 21, 2024, 10:49am UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/8 "2024-01-21T10:49:29Z")

</div>

OK, reusing your loops you could add one extra one _[edit: fixed a typo]_:

```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

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], [22, 22, 22], [32, 32, 32], [42, 42, 42]]
 [[12, 12, 12], [22, 22, 22], [32, 32, 32], [42, 42, 42]]

```

---

<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, 12:00pm UTC](https://discourse.julialang.org/t/vectors-of-vectors-concatenation/109061/9 "2024-01-21T12:00:54Z")

</div>

```julia
res=[]
for p in [first,last,first,last]
  push!(res,p.(last.(v)))
end

```
