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 determinantad - bc: how much A scales areas.
Composing two transformations = multiplying their matrices.
%*% 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.
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 COLUMNSS %*% 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:
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 2B <-matrix(c(0,-1, 1,0), 2, byrow =TRUE) # rotate 90 degA %*% B # rotate THEN scaleB %*% 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 COLUMNSdata <-matrix(c( 1, 0,0, 1,-1, 0,0,-1), nrow =2)theta <- pi/4R <-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 2round(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.