Skip to content

Matrices

A matrix is a rectangular grid of numbers. That sounds humble, but it is the single most important object in all of machine learning. A matrix can be data (a spreadsheet, an image, a batch of feature vectors) and it can be an action (a rotation, a projection, a neural network layer). Learning to see both faces of a matrix is the key skill of this page.

A real-life example: a grayscale image

Open any grayscale photo and zoom in far enough and you’ll find a grid of brightness values, each from 0 (black) to 255 (white). A 1080×1920 photo is literally a matrix with 1080 rows and 1920 columns. Every image filter you’ve ever used — blur, sharpen, edge-detect — is matrix arithmetic on that grid.

Definition

A real-valued (m,n)(m, n) matrix AA has mm rows and nn columns:

A=[a11a12a1na21a22a2nam1am2amn],aijR.A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}, \qquad a_{ij} \in \mathbb{R}.

The entry aija_{ij} sits in row ii, column jj. A (1,n)(1, n) matrix is a row vector; an (m,1)(m, 1) matrix is a column vector. The set of all real (m,n)(m,n) matrices is written Rm×n\mathbb{R}^{m \times n}.

Addition: element by element

Two matrices of the same shape add entry-by-entry:

A+B=[a11+b11].A + B = \begin{bmatrix} a_{11}+b_{11} & \cdots \\ \vdots & \ddots \end{bmatrix}.

That’s it — no surprises. Scaling is just as simple: λA\lambda A multiplies every entry by λ\lambda.

Multiplication: the one that trips everyone up

Matrix multiplication is not element-wise. To multiply ARm×nA \in \mathbb{R}^{m\times n} by BRn×kB \in \mathbb{R}^{n \times k}, each entry of the product C=ABC = AB is a dot product of a row of AA with a column of BB:

cij=l=1nailblj,i=1,,m,    j=1,,k.c_{ij} = \sum_{l=1}^{n} a_{il}\, b_{lj}, \qquad i = 1,\dots,m,\;\; j = 1,\dots,k.

The inner dimensions must match — the number of columns of AA must equal the number of rows of BB — and the result has the outer dimensions:

diagram Diagram mermaid

Worked example

For A=[123321]A = \begin{bmatrix} 1 & 2 & 3\\ 3 & 2 & 1\end{bmatrix} and B=[021101]B = \begin{bmatrix} 0 & 2\\ 1 & -1\\ 0 & 1\end{bmatrix}:

AB=[2325]R2×2,BA=[642202321]R3×3.AB = \begin{bmatrix} 2 & 3\\ 2 & 5 \end{bmatrix} \in \mathbb{R}^{2\times 2}, \qquad BA = \begin{bmatrix} 6 & 4 & 2\\ -2 & 0 & 2\\ 3 & 2 & 1 \end{bmatrix} \in \mathbb{R}^{3\times 3}.

Same two matrices — different shapes out. That alone proves ABBAAB \neq BA.

The identity, inverse, and transpose

The identity matrix InI_n has 1s on the diagonal and 0s elsewhere; it’s the “do nothing” matrix: ImA=AIn=AI_m A = A I_n = A.

A square matrix AA has an inverse A1A^{-1} if AA1=A1A=IAA^{-1} = A^{-1}A = I. Not every matrix has one — those that do are called invertible / regular / nonsingular. For a 2×22\times 2 matrix there’s a closed form:

A=[abcd]    A1=1adbc[dbca],A = \begin{bmatrix} a & b\\ c & d \end{bmatrix} \;\Longrightarrow\; A^{-1} = \frac{1}{ad - bc}\begin{bmatrix} d & -b\\ -c & a \end{bmatrix},

valid exactly when adbc0ad - bc \neq 0. That quantity adbcad - bc is the determinant; when it’s zero, the matrix collapses the plane and cannot be undone.

The transpose AA^\top flips rows and columns: (A)ij=aji(A^\top)_{ij} = a_{ji}. A matrix is symmetric if A=AA = A^\top. Two identities worth memorizing:

(AB)=BA,(AB)1=B1A1.(AB)^\top = B^\top A^\top, \qquad (AB)^{-1} = B^{-1}A^{-1}.

Both reverse the order — a small fact that saves hours of debugging.

The second face: a matrix bends space

Here’s the idea that unlocks deep learning. Multiplying every point x\mathbf{x} of the plane by a fixed 2×22\times 2 matrix AA transforms the whole plane — it rotates, scales, shears, or reflects it. Watch a unit grid (and the little house on it) get transformed as the matrix morphs between identity, a rotation, a shear, and a scaling:

sketch A 2×2 matrix transforming the plane p5.js
The blue grid and orange shape are multiplied by a 2×2 matrix that cycles through identity, rotation, shear, and scaling. The matrix's four numbers are shown live — watch how they move the basis arrows.

The amber and violet arrows are the images of the basis vectors e1=(1,0)\mathbf{e}_1 = (1,0) and e2=(0,1)\mathbf{e}_2 = (0,1). A matrix is completely described by where it sends the basis vectors — those images are exactly its columns. Hold that thought; it’s the whole content of the Linear Mappings page.

NumPy: all of it in code

matrix_ops.py
import numpy as np
 
A = np.array([[1, 2, 3],
              [3, 2, 1]])
B = np.array([[0, 2],
              [1, -1],
              [0, 1]])
 
print("A @ B =\n", A @ B)          # matrix product (2x2)
print("shape:", (A @ B).shape)
 
# transpose
print("A.T =\n", A.T)
 
# identity, inverse, determinant (square matrices)
M = np.array([[4.0, 7.0],
              [2.0, 6.0]])
print("det(M) =", round(np.linalg.det(M), 4))
print("inv(M) =\n", np.linalg.inv(M))
print("M @ inv(M) =\n", np.round(M @ np.linalg.inv(M), 6))   # ~ identity
matrix_ops.py
import numpy as np
 
A = np.array([[1, 2, 3],
              [3, 2, 1]])
B = np.array([[0, 2],
              [1, -1],
              [0, 1]])
 
print("A @ B =\n", A @ B)          # matrix product (2x2)
print("shape:", (A @ B).shape)
 
# transpose
print("A.T =\n", A.T)
 
# identity, inverse, determinant (square matrices)
M = np.array([[4.0, 7.0],
              [2.0, 6.0]])
print("det(M) =", round(np.linalg.det(M), 4))
print("inv(M) =\n", np.linalg.inv(M))
print("M @ inv(M) =\n", np.round(M @ np.linalg.inv(M), 6))   # ~ identity
text
A @ B =
 [[2 3]
 [2 5]]
shape: (2, 2)
A.T =
 [[1 3]
 [2 2]
 [3 1]]
det(M) = 10.0
inv(M) =
 [[ 0.6 -0.7]
 [-0.2  0.4]]
M @ inv(M) =
 [[1. 0.]
 [0. 1.]]
text
A @ B =
 [[2 3]
 [2 5]]
shape: (2, 2)
A.T =
 [[1 3]
 [2 2]
 [3 1]]
det(M) = 10.0
inv(M) =
 [[ 0.6 -0.7]
 [-0.2  0.4]]
M @ inv(M) =
 [[1. 0.]
 [0. 1.]]

Why this matters for ML

  • A fully-connected neural network layer is literally y=Wx+b\mathbf{y} = W\mathbf{x} + \mathbf{b} — a matrix multiply plus a vector. Stacking layers stacks matrix multiplies.
  • Batching: instead of one vector at a time, we stack many inputs as columns of a matrix and transform them all with a single W @ XW @ X. GPUs exist to do this fast.
  • The transpose shows up every time you backpropagate; the inverse and determinant decide whether a linear system (or a covariance matrix) is well-behaved.

🧪 Try It Yourself

Exercise 1 – Multiply two matrices

Exercise 2 – Show AB ≠ BA

Exercise 3 – Invert a 2×2 matrix

Recap

  • A matrix is an m×nm\times n grid; it is both data and a transformation.
  • Addition/scaling are element-wise; multiplication is rows-times-columns and needs matching inner dimensions.
  • Multiplication is not commutative (ABBAAB \neq BA).
  • The identity does nothing, the inverse undoes, the transpose flips — and (AB)=BA(AB)^\top = B^\top A^\top, (AB)1=B1A1(AB)^{-1} = B^{-1}A^{-1} both reverse order.

Next: the algorithm that actually solves Ax=bA\mathbf{x} = \mathbf{b} and finds inverses — Solving Systems of Linear Equations.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did