Vectors of vectors : Concatenation

Consider this vector

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


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

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

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

This is close:

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]]
1 Like

Can you please do it using for loop?

Note that, the OP writes:

while the last answer has:

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

Also, you can try:

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 collects are usually a spurious waste of allocation/time).

What you get is

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

[[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 .

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

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]]
1 Like
res=[]
for p in [first,last,first,last]
  push!(res,p.(last.(v)))
end
1 Like