logo

How to Directly Define Multidimensional Arrays in Julia 📂Julia

How to Directly Define Multidimensional Arrays in Julia

Explanation

1D arrays (vectors) are defined as follows.

julia> A = [1; 2; 3]
3-element Vector{Int64}:
 1
 2
 3

Here, ; signifies moving to the next element based on the first dimension. By generalizing this, ;; signifies moving to the next element based on the second dimension.

julia> A = [1; 2; 3;; 4; 5; 6]
3×2 Matrix{Int64}:
 1  4
 2  5
 3  6

Similarly, arrays of three dimensions and above can be defined. Note that this code is possible from Julia version 1.7 onwards.

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

[:, :, 2] =
 5  6
 7  8

julia> A = [1 2; 3 4;;; 5 6; 7 8 ;;;; 9 10; 11 12;;; 13 14; 15 16]
2×2×2×2 Array{Int64, 4}:
[:, :, 1, 1] =
 1  2
 3  4

[:, :, 2, 1] =
 5  6
 7  8

[:, :, 1, 2] =
  9  10
 11  12

[:, :, 2, 2] =
 13  14

Environment

  • OS: Windows10
  • Version: Julia 1.7.1