# 3 dimensional arrays

**URL:** <https://discourse.julialang.org/t/3-dimensional-arrays/86295>\
**Category:** New to Julia\
**Created:** [August 24, 2022, 11:34pm UTC](https://discourse.julialang.org/t/3-dimensional-arrays/86295 "2022-08-24T23:34:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alina-Elena](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Alina-Elena](https://discourse.julialang.org/u/Alina-Elena)\
**Post date:** [August 24, 2022, 11:34pm UTC](https://discourse.julialang.org/t/3-dimensional-arrays/86295/1 "2022-08-24T23:34:52Z")

</div>

Hi, I am new to Julia and I am attempting to figure out 3 dimensional arrays. The task at hand is to create a new 3-dimensional array that contains a 10x7x2 matrix in the first column of the third dimension and another 10x7x2 matrix in the second column of the third dimension.

Perhaps I do not understand the structure of a 3-dimensional array. Any help would be much appreciated, thank you.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 24, 2022, 11:53pm UTC](https://discourse.julialang.org/t/3-dimensional-arrays/86295/2 "2022-08-24T23:53:11Z")

</div>

> [@Alina-Elena](#):
>
> 10x7x2 matrix

This “matrix” already sounds 3-dimensional.

> [@Alina-Elena](#):
>
> in first column of the third dimension and another 10x7x2 matrix in the second column of the third dimension.

Column? or the 3rd dimension? Do I understand that you want to put two 3 dimensional “matrix” into another 3 dimensional array?

One way to interpret your statement is perhaps you are just trying to concatenate the two 3-dimensional “matrices” into a single 3-dimensional matrix.

You could do

```julia
julia> A
10×7×2 Array{Int64, 3}:
[:, :, 1] =
 8 9 2 10 4 6 8
 9 1 7 3 8 10 4
 5 5 7 1 9 3 6
 1 9 7 9 1 8 5
 8 9 7 4 8 5 7
 9 3 6 2 8 7 7
 1 7 4 6 7 1 2
 2 4 2 3 7 5 7
 3 5 8 8 2 6 1
 4 6 9 9 2 3 5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
 7 9 8 4 8 3 8
 4 10 3 4 9 4 1
 1 6 10 6 5 3 6
 1 6 9 1 9 10 2
 1 5 1 1 8 8 6
 2 7 7 9 4 9 2
 4 10 7 4 10 1 7
 2 7 3 2 2 3 6
 1 10 2 7 10 3 2
 1 8 1 9 6 6 3

```

Perhaps you are really trying to do create an array of arrays. You could do

```julia
julia> D = reshape([A,B],1,1,2)
1×1×2 Array{Array{Int64, 3}, 3}:
[:, :, 1] =
 [8 9 … 6 8; 9 1 … 10 4; … ; 3 5 … 6 1; 4 6 … 3 5;;; 10 9 … 10 7; 7 10 … 5 2; … ; 3 2 … 2 2; 7 2 … 9 4]

[:, :, 2] =
 [6 8 … 1 3; 7 3 … 5 8; … ; 5 7 … 1 8; 9 7 … 7 8;;; 7 9 … 3 8; 4 10 … 4 1; … ; 1 10 … 3 2; 1 8 … 6 3]

julia> D[1]
10×7×2 Array{Int64, 3}:
[:, :, 1] =
 8 9 2 10 4 6 8
 9 1 7 3 8 10 4
 5 5 7 1 9 3 6
 1 9 7 9 1 8 5
 8 9 7 4 8 5 7
 9 3 6 2 8 7 7
 1 7 4 6 7 1 2
 2 4 2 3 7 5 7
 3 5 8 8 2 6 1
 4 6 9 9 2 3 5

[:, :, 2] =
 10 9 4 10 7 10 7
  7 10 2 5 8 5 2
  2 8 8 2 6 5 4
  4 5 3 4 10 8 6
  9 4 2 2 9 8 7
  2 10 1 8 9 5 8
  6 7 7 3 5 2 4
  3 1 7 3 4 7 5
  3 2 4 10 10 2 2
  7 2 7 7 3 9 4

julia> D[2]
10×7×2 Array{Int64, 3}:
[:, :, 1] =
 6 8 5 8 9 1 3
 7 3 2 2 6 5 8
 3 2 8 3 10 6 9
 1 3 2 3 1 2 9
 5 3 1 10 2 7 10
 1 10 1 10 3 5 4
 9 8 9 8 2 7 3
 6 5 9 7 2 4 5
 5 7 3 4 9 1 8
 9 7 2 8 7 7 8

[:, :, 2] =
 7 9 8 4 8 3 8
 4 10 3 4 9 4 1
 1 6 10 6 5 3 6
 1 6 9 1 9 10 2
 1 5 1 1 8 8 6
 2 7 7 9 4 9 2
 4 10 7 4 10 1 7
 2 7 3 2 2 3 6
 1 10 2 7 10 3 2
 1 8 1 9 6 6 3

```

---

<div class="post-metadata">

**Author:** ![Alina-Elena](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Alina-Elena](https://discourse.julialang.org/u/Alina-Elena)\
**Post date:** [August 24, 2022, 11:57pm UTC](https://discourse.julialang.org/t/3-dimensional-arrays/86295/3 "2022-08-24T23:57:11Z")

</div>

That is my mistake, the two previous matrices are just 10x7. Here is the exact text:  
" Create a new array called F which is 3-dimensional and contains A in the first column of the third dimension and B in the second column of the third dimension." - where A and B are two previously defined 10x7 matrices.  
Thank you for your help, this makes a lot more sense.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 25, 2022, 12:27am UTC](https://discourse.julialang.org/t/3-dimensional-arrays/86295/4 "2022-08-25T00:27:18Z")

</div>

```julia
julia> A = rand(1:9, 10, 7)
10×7 Matrix{Int64}:
 1 3 5 3 5 7 5
 4 2 3 8 5 7 7
 8 3 6 7 6 5 1
 6 2 7 6 9 9 8
 3 8 6 8 5 9 8
 6 4 6 9 4 4 4
 2 6 1 9 5 8 6
 6 4 5 4 8 9 7
 1 8 3 6 4 7 8
 9 3 9 6 8 4 4

julia> B = rand(1:9, 10, 7)
10×7 Matrix{Int64}:
 5 1 6 8 6 3 8
 4 6 2 9 7 4 4
 4 7 5 5 3 7 3
 3 3 5 6 3 7 6
 4 3 6 1 1 7 8
 4 4 2 1 3 3 5
 6 4 5 2 1 8 2
 4 6 9 8 4 2 4
 6 7 5 1 9 6 6
 2 9 5 3 5 2 6

julia> C = cat(A, B, dims=3)
10×7×2 Array{Int64, 3}:
[:, :, 1] =
 1 3 5 3 5 7 5
 4 2 3 8 5 7 7
 8 3 6 7 6 5 1
 6 2 7 6 9 9 8
 3 8 6 8 5 9 8
 6 4 6 9 4 4 4
 2 6 1 9 5 8 6
 6 4 5 4 8 9 7
 1 8 3 6 4 7 8
 9 3 9 6 8 4 4

[:, :, 2] =
 5 1 6 8 6 3 8
 4 6 2 9 7 4 4
 4 7 5 5 3 7 3
 3 3 5 6 3 7 6
 4 3 6 1 1 7 8
 4 4 2 1 3 3 5
 6 4 5 2 1 8 2
 4 6 9 8 4 2 4
 6 7 5 1 9 6 6
 2 9 5 3 5 2 6

```

Also see the `[A ;;; B]` syntax which is relatively new.
