# What is the best way to concat an array of tensors

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560>\
**Category:** General Usage\
**Created:** [October 20, 2018, 10:11am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560 "2018-10-20T10:11:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiucheng\_Li](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiucheng_li/32/9773_2.png) [@Xiucheng\_Li](https://discourse.julialang.org/u/Xiucheng_Li)\
**Post date:** [October 20, 2018, 10:11am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560/1 "2018-10-20T10:11:39Z")

</div>

Assuming I have an array of tensors, I would like to concat them along their fourth dimension such as

```julia
xs = [rand(28, 28, 1, 1) for _ in 1:60000]
cat(xs..., dims=4) |> size

```

It will take up to 40GB memory and will not stop within 10mins (I have to end it), so I am wondering what’s the correct way to achieve this?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 20, 2018, 11:24am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560/2 "2018-10-20T11:24:15Z")

</div>

You can do `reduce((xs,x)->cat(xs,x; dims=4), xs)`  
But preallocating an arrays and assigning it with a loop would be far more efficient.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 20, 2018, 11:32am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560/3 "2018-10-20T11:32:46Z")

</div>

> [@baggepinnen](#):
>
> But preallocating an arrays and assigning it with a loop would be far more efficient.

```julia
julia> xs = [rand(28, 28, 1, 1) for _ in 1:60000];

julia> X = zeros(28, 28, 1, 60000);

julia> for i in 1:length(xs)
           X[:, :, 1:1, i:i] .= xs[i]
       end

```

---

<div class="post-metadata">

**Author:** ![Xiucheng\_Li](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiucheng_li/32/9773_2.png) [@Xiucheng\_Li](https://discourse.julialang.org/u/Xiucheng_Li)\
**Post date:** [October 20, 2018, 11:46am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560/4 "2018-10-20T11:46:10Z")

</div>

Thanks. It seems `reduce((xs,x)->cat(xs,x; dims=4), xs)` is still very slow. Maybe I should adopt preallocating and assigning.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 20, 2018, 11:47am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-concat-an-array-of-tensors/16560/5 "2018-10-20T11:47:36Z")

</div>

`reduce(hcat, ...` and `reduce(vcat, ...` have special efficient methods, but this does not exist for higher dimensions.
