Matrices as transformations

Week 1 — Matrices as transformations

Quantitative biology and integrative omics

About this lecture

In Vectors, dot product & projection a vector was a point/arrow (a sample). Today we learn what a matrix does to vectors.

  • A matrix is a function that maps vectors to vectors.
  • The matrix–vector product A\mathbf{x} is a linear combination of the columns of A — and each output entry is a dot product (the tool from Vectors, dot product & projection).
  • Special transformations: identity, scaling, rotation.
  • The determinant ad - bc: how much A scales areas.
  • Composing two transformations = multiplying their matrices.
  • All of it in R with %*%.

This is a hands-on R walkthrough day: follow along, run every block, and watch points move.

A matrix is a function on vectors

A function takes an input and returns an output. A matrix takes a vector in and gives a vector out:

\mathbf{x} \ \longrightarrow\ A\mathbf{x}

Example — take a 2-D point and transform it: A = \begin{pmatrix} 2 & 0 \\ 0 & 3 \end{pmatrix}, \quad \mathbf{x} = \begin{pmatrix} 1 \\ 1 \end{pmatrix} \ \Rightarrow\ A\mathbf{x} = \begin{pmatrix} 2 \\ 3 \end{pmatrix}

  • Here A stretches the x-axis by 2 and the y-axis by 3.
  • Think of A acting on every sample point at once — a uniform operation applied to a whole dataset.

Each entry of A\mathbf{x} is the dot product of a row with \mathbf{x}

Each output entry is the dot product of a matrix row with \mathbf{x}:

\begin{pmatrix} a & b \\ c & d \end{pmatrix}\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} ax + by \\ cx + dy \end{pmatrix}

Worked example: \begin{pmatrix} 1 & 2 \\ 0 & 1 \end{pmatrix}\begin{pmatrix} 3 \\ 4 \end{pmatrix} = \begin{pmatrix} 1\cdot3 + 2\cdot4 \\ 0\cdot3 + 1\cdot4 \end{pmatrix} = \begin{pmatrix} 11 \\ 4 \end{pmatrix}

  • Row 1 of A dotted with \mathbf{x} → first output entry.
  • Row 2 of A dotted with \mathbf{x} → second output entry.

A\mathbf{x} is also a weighted sum of the columns of A

There is a second, equally important way to read A\mathbf{x}: as a linear combination of the columns of A, weighted by the entries of \mathbf{x}.

\begin{pmatrix} 1 & 2 \\ 0 & 1 \end{pmatrix}\begin{pmatrix} 3 \\ 4 \end{pmatrix} = 3\begin{pmatrix} 1 \\ 0 \end{pmatrix} + 4\begin{pmatrix} 2 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 0 \end{pmatrix} + \begin{pmatrix} 8 \\ 4 \end{pmatrix} = \begin{pmatrix} 11 \\ 4 \end{pmatrix}

  • “3 units of column 1, plus 4 units of column 2.”
  • So the output is always a blend of the columns of A.

In R, multiply matrices with %*%, not *

A <- matrix(c(1, 2,
              0, 1), nrow = 2, byrow = TRUE)
x <- c(3, 4)
A %*% x
#      [,1]
# [1,]   11
# [2,]    4
  • %*% is matrix multiplication. Plain * would be elementwise — wrong here.
  • byrow = TRUE fills the matrix row by row, so it matches how we write it on paper. (Default is column by column!)

The identity matrix leaves every vector unchanged

I = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}, \qquad I\mathbf{x} = \mathbf{x} \ \text{for every } \mathbf{x}

Written out for \mathbf{x} = \begin{pmatrix} a \\ b \end{pmatrix}:

I\mathbf{x} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\begin{pmatrix} a \\ b \end{pmatrix} = \begin{pmatrix} 1\cdot a + 0\cdot b \\ 0\cdot a + 1\cdot b \end{pmatrix} = \begin{pmatrix} a \\ b \end{pmatrix}

The 1s pick out each entry and the 0s ignore the other one. The identity leaves every vector unchanged — the matrix version of multiplying by 1.

I <- diag(2)      # 2x2 identity
I
#      [,1] [,2]
# [1,]    1    0
# [2,]    0    1
I %*% c(3, 4)     # 3 4  (unchanged)

A diagonal matrix stretches each axis on its own

A diagonal matrix stretches each axis independently:

S\begin{pmatrix} a \\ b \end{pmatrix} = \begin{pmatrix} 2 & 0 \\ 0 & 0.5 \end{pmatrix}\begin{pmatrix} a \\ b \end{pmatrix} = \begin{pmatrix} 2\cdot a + 0\cdot b \\ 0\cdot a + 0.5\cdot b \end{pmatrix} = \begin{pmatrix} 2a \\ 0.5\,b \end{pmatrix}

The 0s mean the new x depends only on the old x, and the new y only on the old y: double the x-axis, halve the y-axis.

S <- diag(c(2, 0.5))
pts <- matrix(c(1, 1,
                2, 0,
                0, 3), nrow = 2)   # 3 points as COLUMNS
S %*% pts
#      [,1] [,2] [,3]
# [1,]    2    4  0.0    # x doubled
# [2,]  0.5    0  1.5    # y halved
  • Putting samples in columns lets one %*% transform the whole dataset at once.
  • Biology: this is exactly a per-axis normalisation / rescaling of features.

A rotation matrix turns vectors without changing their length

To rotate every point by angle \theta (counter-clockwise) about the origin:

R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}

theta <- pi/2                       # 90 degrees
R <- matrix(c(cos(theta), -sin(theta),
              sin(theta),  cos(theta)), nrow = 2, byrow = TRUE)
round(R %*% c(1, 0))                # 0 1  -> x-axis rotates onto y-axis
  • A rotation changes direction but never length (it is rigid).
  • The columns of R are where the old axes land — a recurring theme.

The columns of a matrix show where the x- and y-axes land

The columns of any matrix A tell you where the standard axes go:

A = \begin{pmatrix} \color{red}{a} & \color{blue}{b} \\ \color{red}{c} & \color{blue}{d} \end{pmatrix}

  • A\begin{pmatrix}1\\0\end{pmatrix} = \color{red}{(a,c)} → first column = image of the x-axis unit vector.
  • A\begin{pmatrix}0\\1\end{pmatrix} = \color{blue}{(b,d)} → second column = image of the y-axis unit vector.

To design a transformation: decide where the axes should land, then write those as the columns.

The determinant tells how much a matrix scales areas

The two columns of A span a parallelogram: the image of the unit square. Its area is the determinant \det A = ad - bc.

  • \lvert \det A \rvert: how much A scales areas (left: 3 times).
  • Negative: the picture is also flipped (middle).
  • Zero: the columns are parallel, the plane collapses onto a line, and A cannot be undone (right).

Doing two moves in a different order ends somewhere else

Seen from above, a person stands on a grid, facing up. Two instructions: turn left and step forward.

Same two instructions, different order, different place. Transformations are the same: doing B then A is usually not the same as A then B.

Doing B then A is the product AB, and order matters

Do B, then A. Applied to \mathbf{x}: first B\mathbf{x}, then A(B\mathbf{x}) = (AB)\mathbf{x}.

So composition is matrix multiplication, and AB means “apply B first, then A” — right to left.

AB:\quad (AB)_{ij} = (\text{row } i \text{ of } A)\cdot(\text{column } j \text{ of } B)

A <- matrix(c(2,0, 0,1), 2, byrow = TRUE)   # scale x by 2
B <- matrix(c(0,-1, 1,0), 2, byrow = TRUE)  # rotate 90 deg
A %*% B          # rotate THEN scale
B %*% A          # scale THEN rotate -- NOT the same!
  • Order matters: AB \neq BA in general. (Try both above.)

Try it: multiply matrices of any size

Hover over or tap a cell of C: it is row i of A times column j of B.

Try it: drag the columns

One chain of %*% transforms every sample at once

# 4 sample points (2 features each) stored as COLUMNS
data <- matrix(c( 1, 0,
                  0, 1,
                 -1, 0,
                  0,-1), nrow = 2)

theta <- pi/4
R <- matrix(c(cos(theta), -sin(theta),
              sin(theta),  cos(theta)), 2, byrow = TRUE)
S <- diag(c(2, 1))

out <- S %*% (R %*% data)   # rotate 45 deg, then stretch x by 2
round(out, 2)

plot(t(data), xlim=c(-3,3), ylim=c(-3,3), pch=19)   # before (rows = points)
points(t(out), col = "red", pch = 19)               # after

One %*% chain applies the whole pipeline to every sample at once.

Summary

Concept Key fact R
Matrix as function input \mathbf{x}, output A\mathbf{x} A %*% x
Output entry row of A \cdot \mathbf{x} (a dot product!) —
Columns view A\mathbf{x} = linear combination of columns of A —
Identity I\mathbf{x}=\mathbf{x} diag(2)
Scaling diagonal matrix stretches each axis diag(c(2, 0.5))
Rotation rigid; preserves length matrix(c(cos,-sin,sin,cos),2,byrow=TRUE)
Determinant ad - bc = area scaling; 0 = cannot be undone det(A)
Composition apply B then A = AB (order matters!) A %*% B

Next (Transforming images with matrices): the same matrices applied to a whole microscopy image — plus shear, reflection, and why we multiply matrices. Week 2 (Eigenvalues & eigenvectors): some special vectors are only scaled by a transformation — eigenvectors.