One way would be:
julia> M = [rand(3) for i=1:2,j=1:4]
2Ă—4 Matrix{Vector{Float64}}:
[0.456187, 0.302626, 0.00769471] [0.217349, 0.599273, 0.922622] … [0.335926, 0.450889, 0.760988]
[0.337041, 0.520466, 0.108746] [0.244348, 0.393246, 0.185435] [0.77141, 0.882466, 0.125375]
julia> [M[i,j][k] for i=1:2,j=1:4,k=1:3]
2Ă—4Ă—3 Array{Float64, 3}:
[:, :, 1] =
0.456187 0.217349 0.738685 0.335926
0.337041 0.244348 0.921115 0.77141
[:, :, 2] =
0.302626 0.599273 0.633224 0.450889
0.520466 0.393246 0.445012 0.882466
[:, :, 3] =
0.00769471 0.922622 0.87023 0.760988
0.108746 0.185435 0.498452 0.125375
Note that since the comprehension value is M[i,j][k]
, this expression can be changed simply to get the dimensions in different order.
Also, in terms of sizes in different dimensions, a more generic comprehension could be:
[M[i,j][k] for i=axes(M,1),j=axes(M,2),k=eachindex(M[1,1])]
or even:
[M[I][k] for I=CartesianIndices(M),k=eachindex(M[1,1])]
In terms of speed, putting the inner index as the first dimension is faster because of memory layout:
[M[I][k] for k=eachindex(M[1,1]),I=CartesianIndices(M)]