Put vector of vector into vector of vector without for loop


#1

Consider the following minimal example:

A = [Vector{Int}(undef, 5) for i = 1:4]
a = [zeros(Int, 2) for i = 1:4]

I’d like to put a into the first 2 inner indices of A without a for loop. Is that possible?
Something like:

A[:][1:2] = a

but this errors with:

ERROR: DimensionMismatch("tried to assign 4 elements to 2 destinations")

#2
julia> A[1:2] .= a[1:2]; A
4-element Array{Array{Int64,1},1}:
 [0, 0]         
 [0, 0]         
 [1, 2, 3, 4, 5]
 [1, 2, 3, 4, 6]

#3

Thank you, but it is not exactly what I wanted.
Consider A and a like

A = [ones(4) for i = 1:4]
4-element Array{Array{Float64,1},1}:
 [1.0, 1.0, 1.0, 1.0]
 [1.0, 1.0, 1.0, 1.0]
 [1.0, 1.0, 1.0, 1.0]
 [1.0, 1.0, 1.0, 1.0]

julia> a = [zeros(2) for i = 1:4]
4-element Array{Array{Float64,1},1}:
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]
 [0.0, 0.0]

The output I like would be:

4-element Array{Array{Float64,1},1}:
 [0.0, 0.0, 1.0, 1.0]
 [0.0, 0.0, 1.0, 1.0]
 [0.0, 0.0, 1.0, 1.0]
 [0.0, 0.0, 1.0, 1.0]

#4

I get,

ERROR: UndefVarError: .= not defined


#5

Are you still using Julia 0.4? I highly recommend you upgrade.