How to concatenate array of tuples containing arrays in Julia
Let's say I have an array x
like this:
x = [(i*ones(4,4,3),rand(11),rand(1:10)) for i=1:5];
Now I want to combine them from the last dimension. I mean, at the end of the operation, I want to have 3 arrays. The size of the first array should be (4,4,3,5)
[concatenation of 5 units (4,4,3)), the second (11.5), and the last one (1.5). How can I do this in Julia?
EDIT Surely I can do it like below, but I want to hear if there is a smart way (in terms of memory consumption and speed):
julia> i=[ t[1] for t in x];
julia> q=[ t[2] for t in x];
julia> l=[ t[3] for t in x];
julia> (cat(4,i...),cat(2,q...),reshape(l,1,length(l))
source to share
Another way:
ntuple(s->reshape(
[x[i][s][j] for j in eachindex(first(x)[s]), i=1:length(x)],
size(first(x)[s])..., length(x)
), length(first(x)))
which saves a bit of time and memory (depending on the sizes / shapes in x
), but the longer solution in the question should be ok. BTW, because this version works for different shapes and lengths x
(as opposed to the version in the question), it looks a little cryptic.
source to share