Convert Any[Any[Any[]]] to 3D array?

I have some data that is of type Array{Float64, 3} and has the same shape as zeros(4, 25, 25). It is stored in JSON. But when I read it back from JSON, the data type becomes Any[Any[Any]]. What is the idiomatic way of convert Any[Any[Any]] to Array{Float64, 3}? Thanks!

Welcome! This works:

julia> a = [[[i+2*(j-1)+6*(k-1) for i=1:2] for j=1:3] for k=1:4]                                                                                                        
4-element Array{Array{Array{Int64,1},1},1}:                                                                                                                             
 [[1, 2], [3, 4], [5, 6]]                                                                                                                                               
 [[7, 8], [9, 10], [11, 12]]                                                                                                                                            
 [[13, 14], [15, 16], [17, 18]]                                                                                                                                         
 [[19, 20], [21, 22], [23, 24]]                                                                                                                                         

julia> reshape(hcat(hcat(a...)...), (4,3,2))                                                                                                                            
4×3×2 Array{Int64,3}:                                                                                                                                                   
[:, :, 1] =                                                                                                                                                             
 1  5   9                                                                                                                                                               
 2  6  10                                                                                                                                                               
 3  7  11                                                                                                                                                               
 4  8  12                                                                                                                                                               

[:, :, 2] =                                                                                                                                                             
 13  17  21                                                                                                                                                             
 14  18  22                                                                                                                                                             
 15  19  23                                                                                                                                                             
 16  20  24                                                                                                                                                             

julia> ans[:]'                                                                                                                                                          
1×24 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:                                                                                                                       
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24                                                                                  

although I suspect there are more clever ways to do it. Note, the hcat seems to do the conversion as well.

PS: please quote your code: PSA: how to quote code with backticks

1 Like

You can also just flatten twice:

# make an example
l1 = ones(2)
l2 = [l1, l1, l1]
l3 = [l2, l2, l2, l2]
# this is the code you want
collect(Iterators.flatten(Iterators.flatten(l3)))
1 Like