A matrix is a grid of numbers with rows and columns. A 2×3 matrix has 2 rows and 3 columns. In Python it is a list of rows: [[1, 2, 3], [4, 5, 6]].
Multiplying matrices
Entry (i, j) of A·B is row i of A dotted with column j of B. So the rows of A must be as long as the columns of B:
(m×n) times (n×p) gives m×p
A 2×3 matrix times a 3×4 matrix is 2×4. A 2×3 times a 2×3 cannot be multiplied at all.
A = [[1, 2],
[3, 4]]
B = [[0, 1],
[1, 0]]
def matmul(X, Y):
return [[sum(X[i][k] * Y[k][j] for k in range(len(Y))) for j in range(len(Y[0]))]
for i in range(len(X))]
print("A·B =", matmul(A, B))
print("B·A =", matmul(B, A))
print("A times the column (1, 1) =", matmul(A, [[1], [1]]))Order matters
In the example, B swaps things around. A·B swaps the columns of A, while B·A swaps its rows: two different answers. For matrices, A·B and B·A are usually different.
The identity
The identity matrix I has 1s on the diagonal and 0s elsewhere. A·I = I·A = A, just as 1 × x = x.
Why it matters
A matrix times a vector transforms the vector: rotating a game character, applying a filter to an image, or computing one layer of a neural network. Graphics cards are built to do millions of these multiplications per second.
Common mistakes
- Multiplying entry by entry (A[i][j] × B[i][j]). That is a different operation, not matrix multiplication.
- Checking the wrong sizes. The inner sizes must match: (2×3)(3×4).
Quick check
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.