# How to stack two "cell arrays" together

**URL:** <https://discourse.julialang.org/t/how-to-stack-two-cell-arrays-together/97557>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 16, 2023, 9:22pm UTC](https://discourse.julialang.org/t/how-to-stack-two-cell-arrays-together/97557 "2023-04-16T21:22:45Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 22, 2023, 4:45pm UTC](https://discourse.julialang.org/t/how-to-stack-two-cell-arrays-together/97557/22 "2023-04-22T16:45:16Z")

</div>

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
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)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 24, 2023, 11:36am UTC](https://discourse.julialang.org/t/how-to-stack-two-cell-arrays-together/97557/23 "2023-04-24T11:36:43Z")

</div>

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

```julia
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

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

```

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

```julia
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

```

[Previous page](https://discourse.julialang.org/t/how-to-stack-two-cell-arrays-together/97557.md?page=1)
