Reputation: 2844
I have an array of multi-dim arrays Array{Array{Float64,3},1}
and what I want is a single 4 dimensional array Array{Float64,4}
.
I have gone through the other responses
But no combination of cat
and reshape
seems to do the trick.
There must be a good idiomatic way... what is it?
Upvotes: 2
Views: 2016
Reputation: 10117
Your answer is correct and generic. Note, however, that assuming your inner arrays have the same size (not just same dimensionality), there is also the following faster way:
julia> matrix = [rand(1,2,3) for _ in 1:4]; # some test data
julia> @btime a = cat($matrix..., dims=4); # your solution
11.519 μs (80 allocations: 3.83 KiB)
julia> @btime b = reshape(collect(Iterators.flatten($matrix)), (1,2,3,4)); # much faster solution
611.960 ns (55 allocations: 2.27 KiB)
julia> a == b
true
Upvotes: 4
Reputation: 2844
Sorry to bother you, I figured it out soon after posting
julia> typeof(matrix)
Array{Array{Float64,3},1}
julia> typeof(matrix[1])
Array{Float64,3}
julia> typeof(cat(matrix...,dims=4))
Array{Float64,4}
Upvotes: 1