Given an array as follows:
A = Array{Array{Int}}(2,2)
A[1,1] = [1,2]
A[1,2] = [3,4]
A[2,1] = [5,6]
A[2,2] = [7,8]
We then have that A is a 2x2 array with elements of type Array{Int}:
2×2 Array{Array{Int64,N} where N,2}:
[1, 2] [3, 4]
[5, 6] [7, 8]
It is possible to access the entries with e.g. A[1,2] but A[1,2,2] would not work since the third dimension is not present in A. However, A[1,2][2] works, since A[1,2] returns an array of length 2.
The question is then, what is a nice way to convert A into a 3-dimensional array, B, so that B[i,j,k] refers the the i,j-th array and the k-th element in that array. E.g. B[2,1,2] = 6.
There is a straightforward way to do this using 3 nested loops and reconstructing the array, element-by-element, but I'm hoping there is a nicer construction. (Some application of cat perhaps?)
cat(3,first.(A),last.(A))also gives the same matrix. If more than two elements in internal vectors,getindexcan be used to generalize this construction.B = permutedims(reshape(collect(Iterators.flatten(A)), (2,2,2)), [3,2,1]), but it is very badly readable and I'd rather use the generators from @durand's answer below. To generalise your answer:cat(3, map(idx -> getindex.(A, idx),(1:2))...)may be alright (for suitable ranges).permutedims(reshape(vcat(A...),2,2,2),[2,3,1])andcat(3,(getindex.(A,i) for i=1:2)...)