2

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?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
opus111
  • 2,744
  • 4
  • 25
  • 41

2 Answers2

4

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
carstenbauer
  • 9,817
  • 1
  • 27
  • 40
1

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}
opus111
  • 2,744
  • 4
  • 25
  • 41