# How to append 3 dimension array( matrix) for example in MATLAB M(:,:,i) = b

**URL:** <https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005>\
**Category:** General Usage\
**Tags:** matlab, array\
**Created:** [July 29, 2022, 10:22pm UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005 "2022-07-29T22:22:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [July 29, 2022, 10:22pm UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/1 "2022-07-29T22:22:04Z")

</div>

How to append 3 dimension array( matrix) for example in MATLAB M(:,:,i) = b.  
and i don’t know how far ‘i’ will go. so please give solution for initial matrix is blank rather than zeros.  
Thank You.

---

<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:** [July 29, 2022, 10:49pm UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/2 "2022-07-29T22:49:26Z")

</div>

You use the `cat` function with the `dims` keyword ([Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.cat)):

```julia
C = cat(A, B; dims=3)

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 29, 2022, 10:53pm UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/3 "2022-07-29T22:53:11Z")

</div>

Another alternative is [ElasticArrays.jl](https://github.com/JuliaArrays/ElasticArrays.jl). The example in the README is similar to what you describe in your post.

---

<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:** [July 30, 2022, 7:11am UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/4 "2022-07-30T07:11:46Z")

</div>

```julia
A=rand(1:10, 2,3,2)
B=rand(1:10, 2,3,2)

[A;;;B]  

```

```julia

A=rand(1:10, 2,3,2,1)
B=rand(1:10, 2,3,2,2)

[A;;;;B]

```

---

<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:** [July 30, 2022, 9:34am UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/5 "2022-07-30T09:34:22Z")

</div>

> [@Starpower\_Power](#):
>
> so please give solution for initial matrix is blank rather than zeros.

To handle concatenation of empty matrices you may need to define a user function. Something like `cat2()` below, just to give an idea:

```julia
cat2(A,B) = isempty(B) ? A : isempty(A) ? B : [A;;;;B]

```

---

<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:** [July 30, 2022, 9:54am UTC](https://discourse.julialang.org/t/how-to-append-3-dimension-array-matrix-for-example-in-matlab-m-i-b/85005/6 "2022-07-30T09:54:27Z")

</div>

I hadn’t paid enough attention to the specific request.  
By modifying the @DNF solution in a “dirty” way, maybe you get the desired result (I have not done many tests)

```julia
cat(A, B ; dims=1+!isempty(A)*2)

```
