How to stack two "cell arrays" together

I left it to show that the creation of C weighs little on the execution time, even if, of course, it allocates an array of 360*180*62 floata64 =>

julia> (180*360*62*8)/(1024*1024) = 30.65185546875 MB

PS
I’m waiting for someone to figure out how to use Cartesian coordinates to see the comparison.

the following is the only way i found to make the method work with cartesianindices

julia> CI(k)=CartesianIndices((axes(A[1])...,k))
CI (generic function with 1 method)

julia> @btime  foreach(k->copyto!($C,CI(k:k), reshape($A[k], 360,180,1), CartesianIndices((360,180,1))), 1:27)
  1.705 ms (243 allocations: 8.44 KiB)

julia> @btime foreach(k->copyto!($C,CI(k:k), reshape($B[k-27], 360,180,1), CartesianIndices((360,180,1))), 28:62)
  2.338 ms (315 allocations: 10.94 KiB)


what do you think of a solution like the following (freely inspired by Tensorcast)?

ulia> begin
           A = [rand(360, 180) for _ in 1:27]
           B = [rand(360, 180) for _ in 1:35]
       end;

julia> struct Stacked{T,N} <: AbstractArray{T,N}
           slices
       end

julia> stack(xs::AbstractArray{IT,ON}) where {IT<:AbstractArray{T,IN}} where {T,IN,ON} =
           Stacked{T, IN+ON}(xs)
stack (generic function with 1 method)

julia> Base.size(x::Stacked) = (size(first(x.slices))..., size(x.slices)...)

julia> function Base.getindex(x::Stacked{T,N}, i,j,k) where {T,N}
           getindex(getindex(x.slices,k),i,j)
       end

julia> using BenchmarkTools

julia> @btime AA=stack([A;B]);
  104.461 ns (2 allocations: 560 bytes)

julia> AA=stack([A;B])
360×180×62 Stacked{Float64, 3}:
[:, :, 1] =
 0.538459    0.328012   0.813001   …  0.852983   0.546863

there are some limitations.
For instance

AA[1:3,1:3,1:3] #  fails ...

a solution for this specific case, but evidently not a very general solution

julia> function Base.getindex(x::Stacked{T,N}, i,j,k) where {T,N}
    if k isa UnitRange{Int64} || k isa Colon
        sl= k isa Colon ? (1:length(x.slices))  : k
        stack([getindex(getindex(x.slices,ki),i,j) for ki in sl])
    else
        getindex(getindex(x.slices,k),i,j)
    end
end

julia> AA[1:3,1:4,1:2]
3×4×2 Stacked{Float64, 3}:
[:, :, 1] =
 0.557888  0.753461  0.449088  0.404467
 0.926965  0.978652  0.141857  0.0795879
 0.967393  0.45095   0.502097  0.0785559

[:, :, 2] =
 0.749748  0.854338  0.455068  0.0667366
 0.127856  0.597543  0.214791  0.521847
 0.220627  0.882986  0.946118  0.720078

julia> AA[1]
0.5578878643298106

julia> AA[1:3,1:4,1]
3×4 Matrix{Float64}:
 0.557888  0.753461  0.449088  0.404467
 0.926965  0.978652  0.141857  0.0795879
 0.967393  0.45095   0.502097  0.0785559

julia> AA[1:3,1:4,2]
3×4 Matrix{Float64}:
 0.749748  0.854338  0.455068  0.0667366
 0.127856  0.597543  0.214791  0.521847
 0.220627  0.882986  0.946118  0.720078