2.2. Multi-dimensional Arrays#

Arrays can have multiple dimensions, e.g. 2-d arrays can be used to represent matrices. They can be constructed by using spaces between the number in each row, and semi-colons between the rows:

A = [1 2 3; -4 -5 -6]
2×3 Matrix{Int64}:
  1   2   3
 -4  -5  -6
typeof(A)
Matrix{Int64} (alias for Array{Int64, 2})

Note the type Matrix{Int64}, or the equivalent Array{Int64,2}, indicating that this is a 2-d array (of integers). When accessing elements in a mutli-dimensional array, you supply multiple indices separated by commas inside the square brackets:

A[2,3]
-6

Multi-dimensional arrays can be operated on using similar syntax as for 1-d arrays. The length function returns the total number of elements in array:

length(A)
6

and the size function can be used to obtain the size of an array along a given dimension:

size(A,1)
2
size(A,2)
3

This can be used to traverse all the elements in a array like before, e.g. using multi-indices:

sumA = 0
for i = 1:size(A,1)
    for j = 1:size(A,2)
        sumA += A[i,j]
    end
end

println("Sum of the elements in A = ", sumA)
Sum of the elements in A = -9

This is also an example of a nested for-loop. For each value of i, the inner loop with go through all values of j. For example, if the size of A is 2-by-3, the nested loops will produce the values (i,j) = (1,1), (1,2), (1,3), (2,1), (2,2), (2,3).

Nested for-loops can also be written using the following shorter syntax, which puts both of the loop variables and indices on the same line:

sumA = 0
for i = 1:size(A,1), j = 1:size(A,2)
    sumA += A[i,j]
end

println("Sum of the elements in A = ", sumA)
Sum of the elements in A = -9

The in syntax can be used for multi-dimensional arrays as well, if indices are not needed:

sumAsquared = 0
for a in A
    sumAsquared += a^2
end

print("Sum of the squares of the elements in A = ", sumAsquared)
Sum of the squares of the elements in A = 91

2.2.1. Multi-dimensional array creation#

The built-in functions from before extend to arrays of any dimension:

Function

Description

zeros(T, dims...)

an array of size dims of all zeros

ones(T, dims...)

an array of size dims of all ones

trues(n, dims...)

a BitArray of size dims with all values true

falses(n, dims...)

a BitArray of size dims with all values false

Here, dims... means a comma-separated list of the size of the array along each dimension. For example:

C = ones(5,3)               # 5-by-3 matrix with all elements == 1
5×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

Note that Julia distinguishes between 1-d arrays, 2-d column vectors, and 2-d row vectors (unlike MATLAB which does not have pure 1-d arrays):

ones(5)      # 1-d array (can be used as a column vector)
5-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
ones(5,1)    # 2-d array, column vector
5×1 Matrix{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
ones(1,5)    # 2-d array, row vector
1×5 Matrix{Float64}:
 1.0  1.0  1.0  1.0  1.0

New arrays can also be created by concatenation using the square bracket syntax:

D = [A; C]                     # Vertical concatenation - arrays must have same # columns
E = [zeros(5) C]               # Horizontal concatenation - arrays must have same # rows
F = [A ones(2,2); zeros(1,5)]  # General vertical-horizontal concatenation - all dimensions must match
3×5 Matrix{Float64}:
  1.0   2.0   3.0  1.0  1.0
 -4.0  -5.0  -6.0  1.0  1.0
  0.0   0.0   0.0  0.0  0.0

Note that when combining arrays that include ranges, they will often automatically be converted:

G = [1:5 ones(Int64,5) 101:105]
5×3 Matrix{Int64}:
 1  1  101
 2  1  102
 3  1  103
 4  1  104
 5  1  105

2.2.2. Dot-syntax and slices for multi-dimensional arrays#

The dot-syntax works on multi-dimensional arrays just like before. For example:

B = A.^2 .- 3A           # Apply function elementwise
2×3 Matrix{Int64}:
 -2  -2   0
 28  40  54
B = @. A^2 - 3A          # Easier syntax - entire expression evaluated elementwise
2×3 Matrix{Int64}:
 -2  -2   0
 28  40  54

Similarly, slices work as before, except you can now provide a subset of indices for each array dimension:

A[1,:]             # Row 1
3-element Vector{Int64}:
 1
 2
 3
A[:,1]             # Column 1
2-element Vector{Int64}:
  1
 -4
A[:,2:3]           # Columns 2-3
2×2 Matrix{Int64}:
  2   3
 -5  -6

This can also be used to modify parts of the array:

A[:,[1,3]] .= 0          # Set columns 1 and 3 to zero
A
2×3 Matrix{Int64}:
 0   2  0
 0  -5  0
A[:,2:3] = 2A[:,1:2]     # Set columns 2 and 3 to twice of columns 1 and 2
A
2×3 Matrix{Int64}:
 0  0    4
 0  0  -10