Exercise: matrices as transformations
Goal
By the end of this exercise you should be able to:
- multiply a matrix by a vector by hand (row · vector), and read it as a linear combination of columns,
- recognise and build identity, scaling, and rotation matrices,
- read off what a matrix does by looking at where the unit axes land,
- compute the determinant \(ad - bc\) of a \(2\times2\) matrix and read it as an area scaling factor,
- compose transformations by multiplying matrices, and explain why order matters,
- do all of this in R with
%*%, transforming a whole dataset at once.
This is an R-heavy walkthrough day. Do the few by-hand problems for fluency, but spend most of your time running and modifying the R blocks and looking at the plots.
Background
A matrix \(A\) is a function on vectors: it takes \(\mathbf{x}\) and returns \(A\mathbf{x}\).
Row recipe (for computing by hand): each output entry is the dot product of a row of \(A\) 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}\]
Column view (for understanding): \(A\mathbf{x}\) is a linear combination of the columns of \(A\), weighted by the entries of \(\mathbf{x}\).
| Transformation | Matrix |
|---|---|
| Identity (do nothing) | \(\begin{pmatrix}1&0\\0&1\end{pmatrix}\) |
| Scaling (x by \(p\), y by \(q\)) | \(\begin{pmatrix}p&0\\0&q\end{pmatrix}\) |
| Rotation by \(\theta\) | \(\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}\) |
| Reflection (flip x) | \(\begin{pmatrix}-1&0\\0&1\end{pmatrix}\) |
Composition: applying \(B\) then \(A\) equals the single matrix \(AB\) (rightmost acts first). In general \(AB \neq BA\).
By hand
B1. Compute \(\begin{pmatrix} 2 & 1 \\ 0 & 3 \end{pmatrix}\begin{pmatrix} 4 \\ 1 \end{pmatrix}\) using the row recipe.
B2. Redo B1 as a linear combination of the columns of the matrix. Confirm you get the same answer.
B3. What does \(\begin{pmatrix} 3 & 0 \\ 0 & 1 \end{pmatrix}\) do to a point geometrically? Apply it to \((1,1)\), \((2,0)\) and \((0,5)\).
B4. Write down the rotation matrix for \(\theta = 90^\circ\) (use \(\cos 90^\circ = 0\), \(\sin 90^\circ = 1\)). Apply it to the x-axis unit vector \((1,0)\) and to \((0,1)\). Where do the axes land?
B5. Without multiplying anything out, state where the two standard axes \((1,0)\) and \((0,1)\) land under \(A = \begin{pmatrix} 2 & -1 \\ 1 & 3 \end{pmatrix}\). (Hint: look at the columns.)
B6. Let \(A = \begin{pmatrix} 2 & 0 \\ 0 & 1 \end{pmatrix}\) (scale x by 2) and \(B = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}\) (rotate \(90^\circ\)). Compute both \(AB\) and \(BA\) by hand and show they differ.
B7. Let \(B = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}\) (rotate \(90^\circ\)) and \(F = \begin{pmatrix} -1 & 0 \\ 0 & 1 \end{pmatrix}\) (flip x). Write “rotate, then flip” as one matrix. Which product is it, \(FB\) or \(BF\)? Compute it and describe what it does to \((1,0)\) and \((0,1)\).
B8. Let \(A = \begin{pmatrix} 3 & 1 \\ 0 & 2 \end{pmatrix}\). Sketch the parallelogram spanned by its columns and find its area from base × height. Compare with \(\det A = ad - bc\). Then compute \(\det\) of the matrix \(FB\) from B7. What does its sign tell you?
Explore
Drag the red and blue handles: they are the two columns of \(A\), i.e. where \((1,0)\) and \((0,1)\) land. The grid, the unit square, the house and the test vector \(\mathbf{x}\) (grey handle) all follow. The second tab compares the two orders \(AB\) and \(BA\).
E1. Drag the columns to build the matrix from B5, \(\begin{pmatrix} 2 & -1 \\ 1 & 3 \end{pmatrix}\). Predict \(A\mathbf{x}\) for \(\mathbf{x} = (1, 1)\) from the columns, then read it off the demo.
E2. Click Reflect and Rotate 90°. Which one turns the house into a mirror image? Then drag the columns to make the matrix from B7 (rotate, then flip): where must the red and blue handles go?
E3. Click Project. Drag \(\mathbf{x}\) around: where can \(A\mathbf{x}\) end up? Find two different vectors \(\mathbf{x}\) with the same \(A\mathbf{x}\). What does that mean for undoing the transformation?
E4. Open the Order: AB vs BA tab. With A = Scale x ×2 and B = Rotate 90° (the setting of B6), compare the two matrices shown with your hand answer. Then find a pair of presets for which \(AB = BA\).
E1. Red handle at \((2, 1)\), blue handle at \((-1, 3)\). \(A\mathbf{x} = 1\cdot(2,1) + 1\cdot(-1,3) = (1, 4)\): one unit of each column.
E2. Reflect: the door jumps from the left to the right side of the house, so it is a mirror image. Rotate 90° only turns the house; it still reads the same way round. For B7, \(FB = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\): red handle (column 1) at \((0, 1)\), blue handle (column 2) at \((1, 0)\). The house is mirrored in the line \(y = x\).
E3. \(A = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}\) keeps x and sets y to 0, so \(A\mathbf{x}\) always lands on the x-axis. For example \((1, 1)\) and \((1, 2)\) both give \((1, 0)\). Since different inputs give the same output, you cannot tell from the output which input it came from: the projection cannot be undone.
E4. \(AB = \begin{pmatrix} 0 & -2 \\ 1 & 0 \end{pmatrix}\) and \(BA = \begin{pmatrix} 0 & -1 \\ 2 & 0 \end{pmatrix}\), as in B6; the two houses differ. Pairs that give \(AB = BA\): anything with Identity; Rotate 90° with Rotate 30° (two rotations about the origin add up their angles in either order); Scale x ×2 with Flip x (both only act on each axis separately).
In R
Work through every block. Change numbers and re-run to build intuition.
Matrix–vector product
A <- matrix(c(2, 1,
0, 3), nrow = 2, byrow = TRUE)
x <- c(4, 1)
A %*% x # check against B1: 9 3
# elementwise * is NOT matrix multiplication -- see the difference:
A * x # wrong tool here; recycles x down the columnsIdentity and scaling, applied to a whole dataset
Store sample points as columns so one %*% transforms them all.
I <- diag(2)
S <- diag(c(3, 1)) # stretch x by 3, leave y
pts <- matrix(c( 1, 1,
2, 0,
0, 5), nrow = 2) # 3 points as columns
I %*% pts # unchanged
S %*% pts # x-coordinates tripledRotation, with a plot
rot <- function(theta) {
matrix(c(cos(theta), -sin(theta),
sin(theta), cos(theta)), nrow = 2, byrow = TRUE)
}
# a little square of points (as columns)
square <- matrix(c( 1, 1,
-1, 1,
-1,-1,
1,-1), nrow = 2)
R <- rot(pi/4) # 45 degrees
rotated <- R %*% square
plot(t(square), xlim=c(-2,2), ylim=c(-2,2), pch=19, asp=1,
xlab="feature 1", ylab="feature 2")
points(t(rotated), col="red", pch=19)
# black = before, red = after (note: t() because plot wants points as ROWS)Composition: order matters
A <- diag(c(2, 1)) # scale x by 2
B <- rot(pi/2) # rotate 90 deg
scale_then_rotate <- B %*% A # A acts first
rotate_then_scale <- A %*% B # B acts first
round(scale_then_rotate %*% c(1, 0), 3)
round(rotate_then_scale %*% c(1, 0), 3)
# different outputs -> AB != BATasks
R1. In the matrix–vector block, explain in one sentence why A * x gives a different (wrong) result than A %*% x.
R2. Modify the rotation block to rotate by \(30^\circ\) instead of \(45^\circ\) and confirm the square’s corners move but its size does not (a rotation preserves length — check with sqrt(colSums(rotated^2))).
R3. Build a single matrix T that first rotates by \(90^\circ\) and then scales x by 2, as one matrix. Apply it to square in one %*%. (Hint: which of A %*% B / B %*% A is “rotate first”?)
R4. A design-matrix flavour. Let
X <- matrix(c(1, 0,
1, 0,
1, 1,
1, 1), nrow = 4, byrow = TRUE) # 4 samples: 2 control, 2 treated
beta <- c(5, 3) # baseline = 5, treatment effect = +3
X %*% betaRead off the predicted expression for each sample and explain, in terms of the columns of X, why the treated samples come out higher.
Hints
Matrix multiplication is %*%. Plain * is elementwise and will silently recycle and give a wrong-shaped or wrong-valued result. Always use %*% for \(A\mathbf{x}\) and \(AB\).
matrix fills column by column by default. Pass byrow = TRUE to fill row by row so the R matrix matches what you wrote on paper. Always print the matrix to confirm before using it.
diag(2) makes the \(2\times2\) identity. diag(c(p, q)) makes a diagonal scaling matrix (x by p, y by q). Diagonal matrices stretch each axis independently.
\(AB\) means “apply \(B\) first, then \(A\)” (rightmost first). So “rotate then scale” with rotation R and scaling S is S %*% R. In R, just multiply the matrices with %*%, then apply the product to your data.
Solutions
B1. Use the row recipe: each output entry is a row of \(A\) dotted with \(\mathbf x = (4,1)\). Row 1: \(\begin{pmatrix}2 & 1\end{pmatrix}\cdot(4,1) = 2\cdot4 + 1\cdot1 = 8 + 1 = 9\). Row 2: \(\begin{pmatrix}0 & 3\end{pmatrix}\cdot(4,1) = 0\cdot4 + 3\cdot1 = 0 + 3 = 3\). Result: \(\begin{pmatrix}9\\3\end{pmatrix}\).
B2. Now read \(A\mathbf x\) as a linear combination of the columns of \(A\), weighted by the entries of \(\mathbf x = (4,1)\): \[4\begin{pmatrix}2\\0\end{pmatrix} + 1\begin{pmatrix}1\\3\end{pmatrix} = \begin{pmatrix}8\\0\end{pmatrix} + \begin{pmatrix}1\\3\end{pmatrix} = \begin{pmatrix}9\\3\end{pmatrix}.\] Same answer as B1 — the row recipe and the column view always agree; they are two ways of reading the same product.
B3. The matrix \(\begin{pmatrix}3&0\\0&1\end{pmatrix}\) is diagonal, so it scales each axis independently: x by 3, y by 1 (unchanged). Apply it to each point with the row recipe: \((1,1)\): \((3\cdot1+0\cdot1,\ 0\cdot1+1\cdot1) = (3,1)\). \((2,0)\): \((3\cdot2+0\cdot0,\ 0\cdot2+1\cdot0) = (6,0)\). \((0,5)\): \((3\cdot0+0\cdot5,\ 0\cdot0+1\cdot5) = (0,5)\). So it triples the x-coordinate and leaves y unchanged (a horizontal stretch by 3).
B4. With \(\theta = 90^\circ\), \(\cos 90^\circ = 0\) and \(\sin 90^\circ = 1\), so \[R = \begin{pmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{pmatrix} = \begin{pmatrix}0&-1\\1&0\end{pmatrix}.\] Apply to \((1,0)\): \((0\cdot1 + (-1)\cdot0,\ 1\cdot1 + 0\cdot0) = (0,1)\). Apply to \((0,1)\): \((0\cdot0 + (-1)\cdot1,\ 1\cdot0 + 0\cdot1) = (-1,0)\). The x-axis swings up to the y-axis; the y-axis swings to the negative x-axis — a \(90^\circ\) counter-clockwise turn.
B5. No multiplication needed: the columns of \(A\) are exactly the images of the standard axes. Column 1 is where \((1,0)\) lands; column 2 is where \((0,1)\) lands. So \((1,0)\to(2,1)\) (first column of \(A\)) and \((0,1)\to(-1,3)\) (second column of \(A\)).
B6. Compute each product entry by entry; entry \((i,j)\) is row \(i\) of the left matrix dotted with column \(j\) of the right matrix. \[AB = \begin{pmatrix}2&0\\0&1\end{pmatrix}\begin{pmatrix}0&-1\\1&0\end{pmatrix}.\] - \((1,1)\): \(2\cdot0 + 0\cdot1 = 0\). \((1,2)\): \(2\cdot(-1) + 0\cdot0 = -2\). - \((2,1)\): \(0\cdot0 + 1\cdot1 = 1\). \((2,2)\): \(0\cdot(-1) + 1\cdot0 = 0\).
\[AB = \begin{pmatrix}0&-2\\1&0\end{pmatrix}.\] Now the other order: \[BA = \begin{pmatrix}0&-1\\1&0\end{pmatrix}\begin{pmatrix}2&0\\0&1\end{pmatrix}.\] - \((1,1)\): \(0\cdot2 + (-1)\cdot0 = 0\). \((1,2)\): \(0\cdot0 + (-1)\cdot1 = -1\). - \((2,1)\): \(1\cdot2 + 0\cdot0 = 2\). \((2,2)\): \(1\cdot0 + 0\cdot1 = 0\).
\[BA = \begin{pmatrix}0&-1\\2&0\end{pmatrix}.\] Compare: \(AB\) has \(-2\) in the top-right and \(1\) in the bottom-left, while \(BA\) has \(-1\) and \(2\) there. They differ, so \(AB \neq BA\) — order matters. (Geometrically: scaling-x-then-rotating is not the same as rotating-then-scaling-x.)
B7. “Rotate, then flip” means \(B\) acts first, so it goes on the right: \(FB\). \[FB = \begin{pmatrix}-1&0\\0&1\end{pmatrix}\begin{pmatrix}0&-1\\1&0\end{pmatrix} = \begin{pmatrix}(-1)\cdot0 + 0\cdot1 & (-1)\cdot(-1) + 0\cdot0\\ 0\cdot0 + 1\cdot1 & 0\cdot(-1) + 1\cdot0\end{pmatrix} = \begin{pmatrix}0&1\\1&0\end{pmatrix}.\] Its columns say \((1,0)\to(0,1)\) and \((0,1)\to(1,0)\): it swaps x and y, a mirror in the diagonal line \(y = x\). (The other order, \(BF = \begin{pmatrix}0&-1\\-1&0\end{pmatrix}\), mirrors in the line \(y = -x\) instead.)
B8. The columns \((3,0)\) and \((1,2)\) span a parallelogram with base 3 (along the x-axis) and height 2, so its area is \(3 \cdot 2 = 6\). The determinant agrees: \(\det A = 3\cdot2 - 1\cdot0 = 6\), so \(A\) makes every area 6 times larger. For \(FB = \begin{pmatrix}0&1\\1&0\end{pmatrix}\), \(\det = 0\cdot0 - 1\cdot1 = -1\): areas are unchanged (size 1), but the minus sign says the picture is flipped, as expected for a mirror.
R1. A %*% x performs the row-dot-vector operation (true matrix multiplication), giving \((9,3)\) — the result from B1. A * x instead multiplies elementwise, recycling the length-2 vector x down the columns of A, so it computes c(2*4, 0*1, 1*4, 3*1) reshaped — a different operation entirely, not the linear transformation we want.
A <- matrix(c(2, 1, 0, 3), nrow = 2, byrow = TRUE)
x <- c(4, 1)
A %*% x # [,1]
# [1,] 9
# [2,] 3
A * x # elementwise, recycles x: NOT the transformationR2. Change the angle to \(30^\circ\) = pi/6 and compare lengths before and after.
R <- rot(pi/6) # 30 degrees
rotated <- R %*% square
sqrt(colSums(square^2)) # 1.414214 1.414214 1.414214 1.414214 (all sqrt(2))
sqrt(colSums(rotated^2)) # 1.414214 1.414214 1.414214 1.414214 (identical)The corners move to new positions but every column keeps the same length (\(\sqrt2\)), confirming a rotation preserves length (it only reorients, never stretches).
R3. “Rotate first, then scale” means the rotation acts first, so it goes on the right (rightmost acts first): with B the rotation and A the scaling, the single matrix is T <- A %*% B. Then one %*% applies both.
A <- diag(c(2, 1)) # scale x by 2
B <- rot(pi/2) # rotate 90 deg (acts first)
T <- A %*% B # = A %*% B, rotate-then-scale
T %*% square # applies rotation then x-scaling in one stepR4. Multiply out with the row recipe (each sample = one row of X dotted with beta = (5,3)):
X %*% beta # [,1]
# [1,] 5 # (1,0): 1*5 + 0*3
# [2,] 5 # (1,0): 1*5 + 0*3
# [3,] 8 # (1,1): 1*5 + 1*3
# [4,] 8 # (1,1): 1*5 + 1*3So X %*% beta gives c(5, 5, 8, 8). The first column of X is all 1s, so every sample picks up the baseline 5; the second column is 1 only for the treated samples, so they additionally pick up the treatment effect 3, giving 8. Reading it as the column view: the prediction is a linear combination of X’s columns weighted by beta — exactly the matrix-as-transformation idea, here mapping effect sizes to predicted expression. This is why the treated samples come out higher.
Where this leads
- Transforming images with matrices (next): the same matrices applied to every pixel of a microscopy image — plus shear, reflection, and why a pipeline of steps is best written as one product matrix.
- Eigenvalues & eigenvectors / Eigenvalues in R: for a given transformation, are there special directions that get only stretched, never rotated? Those eigenvectors expose the structure of a matrix and are the mathematical core of PCA.
- Markov chains / Markov chains in R: a transition matrix is a transformation applied repeatedly — multiply by the matrix again and again to evolve a distribution forward in time.
- PCA (Week 2): rotating the cloud of samples to new, more informative axes is exactly composing rotation matrices with your data, the operation you practised today.