# Concatenating 2D generator of arrays

**URL:** <https://discourse.julialang.org/t/concatenating-2d-generator-of-arrays/78775>\
**Category:** New to Julia\
**Tags:** splitapplycombine\
**Created:** [March 31, 2022, 1:10am UTC](https://discourse.julialang.org/t/concatenating-2d-generator-of-arrays/78775 "2022-03-31T01:10:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [March 31, 2022, 1:10am UTC](https://discourse.julialang.org/t/concatenating-2d-generator-of-arrays/78775/1 "2022-03-31T01:10:35Z")

</div>

Hi, I’m trying to concatenate an 2D generator that generate an 2D array of 2x2 arrays, such as this one  
`iter = (rand(2,2) for i in 1:d1, j in 1:d2 )`  
I would need to concatenate these arrays into a 2 by `d1` by 2 by `d2` array, ideally w/o first generating an array of wrong permutation and then permute. I tried `cat(iter..., dims=(1,3))` or `cat(iter..., dims=(2,4))`, neither worked.

I also read a little about [this thread](https://discourse.julialang.org/t/very-best-way-to-concatenate-an-array-of-arrays/8672), however, I don’t want to `collect(iter)` before the concatenation or use `LazyArrays` as the latter is probably not as efficient when later used for LA operations. In other words, I want the memory to be allocated exactly as specified dimensions.

So what is the right way to generate this concatenation correctly?

Thank you!

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 31, 2022, 5:48am UTC](https://discourse.julialang.org/t/concatenating-2d-generator-of-arrays/78775/2 "2022-03-31T05:48:23Z")

</div>

If you can afford collecting your `iter` generator:

```julia
julia> using SplitApplyCombine

julia> combinedims([rand(2,2) for i in 1:3, j in 1:5], (2, 4))
2×3×2×5 Array{Float64, 4}:
...

```

If you need to keep the generator lazy and only materialize the final array:

```julia
julia> using SplitApplyCombine, GeneratorArrays

julia> combinedims(array(rand(2,2) for i in 1:3, j in 1:5), (2, 4))
2×3×2×5 Array{Float64, 4}:
...

```

Alternatively, see `mapview()` in `SplitApplyCombine.jl` for lazy arrays.

Note that the first - eager - approach can be faster than the lazy one.
