# 3 dimensional arrays

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.

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> 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

[:, :, 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> B
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

julia> C = [A;;;B]
10×7×4 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

[:, :, 3] =
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

[:, :, 4] =
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> 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
``````

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.

``````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.

1 Like