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