# Optimal way to flatten array

**URL:** <https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923>\
**Category:** General Usage\
**Tags:** array\
**Created:** [April 9, 2021, 3:04pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923 "2021-04-09T15:04:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)\
**Post date:** [April 9, 2021, 3:04pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/1 "2021-04-09T15:04:46Z")

</div>

I have an array of array

```julia
y = [ones(Float32, 3) for i=1:3]
sum(abs2, y)

```

which I can’t apply `abs2` to.  
I need it to compute my loss function

Now I use custom function

```julia
function flat_this(x)
    arr = ones(0)
    for i=1:length(x)
        arr = vcat(arr, x[i])
    end
    return arr
end

```

But I doubt it’s optimal and fast enough

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [April 9, 2021, 3:36pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/2 "2021-04-09T15:36:54Z")

</div>

`reduce(vcat, y)`

or in this case, to avoid an intermediary Array

`sum(i -> sum(abs2,i), y)`

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 9, 2021, 3:48pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/3 "2021-04-09T15:48:11Z")

</div>

Or another alternative:  
`sum(abs2,Iterators.flatten(y))`

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [April 9, 2021, 3:55pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/4 "2021-04-09T15:55:26Z")

</div>

`Iterators.flatten` doesn’t allocate because it’s lazy but it’s slower (I have no idea why)

```julia
julia> y = [rand(100) for i in 1:100];

julia> @btime sum(abs2, reduce(vcat, $y))
  7.200 μs (2 allocations: 78.20 KiB)
3333.509067117525

julia> @btime sum(i -> sum(abs2,i), $y)
  1.400 μs (0 allocations: 0 bytes)
3333.5090671175253

julia> @btime sum(abs2,Iterators.flatten($y))
  9.600 μs (0 allocations: 0 bytes)
3333.5090671175276

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 9, 2021, 5:36pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/5 "2021-04-09T17:36:41Z")

</div>

Another way to flatten `y`, quite inefficient compared to `reduce(vcat,y)` above, though:

```julia
[yi for yr in y for yi in yr]

```

---

<div class="post-metadata">

**Author:** ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)\
**Post date:** [April 9, 2021, 9:39pm UTC](https://discourse.julialang.org/t/optimal-way-to-flatten-array/58923/6 "2021-04-09T21:39:06Z")

</div>

Thanks for replies
