Julia Matrices & Slicing
Julia has true multi-dimensional arrays. A matrix is a 2-D array; higher dimensions work the same way. Matrices are stored in column-major order, like in Fortran and MATLAB: the elements of each column sit next to each other in memory.
Creating matrices
Inside square brackets, spaces separate columns and semicolons (or newlines) separate rows:
A = [1 2 3; 4 5 6]
println(A)
println(typeof(A), " size ", size(A))
println(size(A, 1), " rows, ", size(A, 2), " columns, ", length(A), " elements")
B = [1.0 0.0
0.0 1.0]
println(B)
println(zeros(Int, 2, 3))
println(reshape(1:6, 2, 3))[1 2 3; 4 5 6]
Matrix{Int64} size (2, 3)
2 rows, 3 columns, 6 elements
[1.0 0.0; 0.0 1.0]
[0 0 0; 0 0 0]
[1 3 5; 2 4 6]
reshape(1:6, 2, 3) fills column by column, which shows the column-major order.
Note:
[1, 2, 3](commas) is a vector, while[1 2 3](spaces) is a 1×3 matrix — a row.
Indexing and slicing
Use A[row, col]. A colon : selects a whole dimension. Slices copy the data:
M = [10 20 30
40 50 60
70 80 90]
println(M[2, 3])
println(M[1, :]) # first row (as a vector)
println(M[:, 2]) # second column
println(M[1:2, 2:3]) # a sub-matrix
println(M[end, end])
println(M[5]) # linear index, column-major: 5060
[10, 20, 30]
[20, 50, 80]
[20 30; 50 60]
90
50
Modifying elements and slices
G = zeros(Int, 3, 3)
G[2, 2] = 5
G[1, :] .= 1 # broadcast-assign a whole row
G[:, 3] = [7, 8, 9] # assign a column from a vector
println(G)
G[G .== 0] .= -1 # logical indexing
println(G)[1 1 7; 0 5 8; 0 0 9]
[1 1 7; -1 5 8; -1 -1 9]
Views instead of copies
A slice like M[1, :] allocates a new array. view(M, 1, :) (or the @view macro) returns a lightweight window into the original, so changes go through to M:
M = [1 2; 3 4]
row_copy = M[1, :]
row_view = @view M[1, :]
row_copy[1] = 100
row_view[2] = 200
println(M)
println(sum(@view M[:, 2]))[1 200; 3 4]
204
Combining and transforming
| Function / syntax | Meaning |
|---|---|
vcat(A, B) or [A; B] |
stack vertically |
hcat(A, B) or [A B] |
place side by side |
A' or transpose(A) |
transpose (' is the conjugate transpose) |
sum(A, dims=1) |
sum over rows → a row of column sums |
vec(A) |
flatten to a vector (column-major) |
eachrow(A), eachcol(A) |
iterate over rows/columns |
A = [1 2; 3 4]
println([A; [5 6]])
println([A [9; 9]])
println(A')
println(sum(A, dims=1), " ", sum(A, dims=2))
println(vec(A))
for (i, r) in enumerate(eachrow(A))
println("row ", i, ": ", r, " total ", sum(r))
end
println(A * A, " vs ", A .* A)[1 2; 3 4; 5 6]
[1 2 9; 3 4 9]
[1 3; 2 4]
[4 6] [3; 7;;]
[1, 3, 2, 4]
row 1: [1, 2] total 3
row 2: [3, 4] total 7
[7 10; 15 22] vs [1 4; 9 16]
A * A is matrix multiplication, while A .* A multiplies element by element (see Broadcasting). The odd [3; 7;;] is how Julia prints a 2×1 matrix on one line.
Printing a matrix as a grid
println(A) prints a compact one-line form. For a readable grid, loop over the rows:
board = ['X' 'O' ' '; ' ' 'X' 'O'; 'O' ' ' 'X']
for row in eachrow(board)
println(" ", join(row, " | "))
end X | O |
| X | O
O | | X
Test yourself
What is size([1 2 3; 4 5 6])?
- (3, 2)
- (2, 3)
- 6
- (6,)
Given M = [1 2; 3 4], what is M[:, 1]?
- [1, 2]
- [1, 3]
- [3, 4]
- 1
How does reading M[1, :] differ from view(M, 1, :)?
- Nothing
- Reading
M[1, :]makes a copy;viewrefers to the original data viewonly works on vectorsviewsorts the data