# How to stack up multiple 3-dimension matrices into one?

**URL:** <https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225>\
**Category:** General Usage\
**Tags:** question, arrays\
**Created:** [December 16, 2021, 7:23pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225 "2021-12-16T19:23:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 16, 2021, 7:23pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/1 "2021-12-16T19:23:58Z")

</div>

For example, I could have 10 Matrices like the below (A) stored in 10 NetCDF files:

```julia
A = rand(5, 3, 2);

```

My goal is to read them all out and stack them up into a new matrix B with the size of `size(B) = (5, 3, 20)`.

I tried the below:

```julia
B = fill(NaN, 5, 3, 0);
for i in 1:10
# read A out of a NetCDF file
B = vcat(B, A);
end

```

Unfortunately, I got the below error:  
`DimensionMismatch("mismatch in dimension 3 (expected 0 got 2)")`

I wonder what is the best way to do so?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 16, 2021, 7:32pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/2 "2021-12-16T19:32:22Z")

</div>

In Julia v1.7 you can use repeated `;` to concatenate on higher dimensions:

```julia
[A; A] # first dimension
[A;; A] # second dimension
[A;;; A] # third dimension

```

Notice that `vcat` and `hcat` are the verbose versions of `[A; A]` and `[A A]`.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 16, 2021, 7:34pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/3 "2021-12-16T19:34:40Z")

</div>

Thanks! Unfortunately, that will give me the same error.

I think I’m able to figure it out now:  
B = cat(B, A, dims=3);

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 16, 2021, 7:37pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/4 "2021-12-16T19:37:08Z")

</div>

You shouldn’t be rewriting the content of the variable `B` in a loop. Try to write it in terms of a `reduce` or `mapreduce` instead. It will be cleaner and more efficient. in general.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 16, 2021, 7:40pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/5 "2021-12-16T19:40:31Z")

</div>

> [@leon](#):
>
> Matrices

This is a nitpick, but it may confuse some (maybe it has confused you as well): a matrix is a 2-dimensional array. 3d arrays are not matrices.

(I was going to add a reference, but, depressingly, in the first three pages of Google hits for “matrix”, there were _zero_ results referring to linear algebra…)

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 16, 2021, 7:50pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/6 "2021-12-16T19:50:18Z")

</div>

Many thanks.

Would you mind elaborating on how to use `reduce`?

I’m able to find the documentation but can not make it work:  
[https://docs.julialang.org/en/v1/base/collections/#Base.reduce-Tuple{Any,%20Any}](https://docs.julialang.org/en/v1/base/collections/#Base.reduce-Tuple%7BAny,%20Any%7D)

Something like the below?  
B = reduce(cat, [B, A], dims=3)?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 16, 2021, 7:54pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/7 "2021-12-16T19:54:07Z")

</div>

You should use an anonymous function

```julia
reduce((x, y) -> cat(x, y; dims = 3), vec)

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 16, 2021, 8:08pm UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/9 "2021-12-16T20:08:34Z")

</div>

It works.

Many thanks!

---

<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:** [December 17, 2021, 12:20am UTC](https://discourse.julialang.org/t/how-to-stack-up-multiple-3-dimension-matrices-into-one/73225/10 "2021-12-17T00:20:53Z")

</div>

You could also do (_example for 5 input arrays_):

```julia
B = cat(A1, A2, A3, A4, A5, dims=3)

# or:
A = [A1, A2, A3, A4, A5]
B = cat(A..., dims=3)

```
