Matrix Decompositions
A decomposition (or factorisation) rewrites a matrix as a product of simpler matrices: triangular, orthogonal or diagonal ones. Those simple pieces are cheap to solve with and reveal properties that are hidden in the original numbers, such as rank, sensitivity to rounding or the "important directions" in data. The Linear Algebra lesson listed the four classic factorisations; this lesson uses each one for a real job.
| Function | Factorisation | Requirements | Typical use |
|---|---|---|---|
[L, U, P] = lu(A) |
P*A = L*U |
square | Solving many systems with the same A, determinants |
R = chol(A) |
A = R'*R |
symmetric positive definite | Fast solves, testing positive definiteness |
[Q, R] = qr(A, 0) |
A = Q*R |
any shape | Least squares, orthonormal bases |
[U, S, V] = svd(A) |
A = U*S*V' |
any shape | Rank, condition number, low-rank approximation |
[V, D] = eig(A) |
A*V = V*D |
square | Vibration modes, matrix powers, stability |
Note: Several factorisations are only unique up to signs: a column of
Q,U,Vor an eigenvector can come out multiplied by −1, and MATLAB and Octave may choose differently. Both answers are correct. The examples below therefore print things that don't depend on the sign: reconstruction errors, singular values, eigenvalues and absolute values.
LU: factor once, solve many times
Gaussian elimination secretly computes an LU decomposition: a lower-triangular L (with ones on the diagonal), an upper-triangular U, and a permutation P that records the row swaps (partial pivoting) made for numerical stability. Solving with triangular matrices is cheap, so when the same A meets many right-hand sides, factor it once and reuse the factors:
A = [2 1 1; 4 3 3; 8 7 9];
[L, U, P] = lu(A)
fprintf('P*A equals L*U: %d\n', norm(P*A - L*U) < 1e-12);
B = [4 1; 10 0; 24 2]; % two right-hand sides
for k = 1:size(B, 2)
y = L \ (P * B(:, k)); % forward substitution
x = U \ y; % back substitution
fprintf('solution %d: %s\n', k, mat2str(round(x' * 1e4) / 1e4));
end
fprintf('det from U: %g, det(A): %g\n', det(P) * prod(diag(U)), det(A));L =
1.0000 0 0
0.2500 1.0000 0
0.5000 0.6667 1.0000
U =
8.0000 7.0000 9.0000
0 -0.7500 -1.2500
0 0 -0.6667
P =
Permutation Matrix
0 0 1
1 0 0
0 1 0
P*A equals L*U: 1
solution 1: [1 1 1]
solution 2: [1.5 -4 2]
det from U: 4, det(A): 4
Octave labels P as a "Permutation Matrix" (it stores it in a compact special form); MATLAB shows it as an ordinary matrix of zeros and ones. Backslash recognises triangular matrices and uses substitution directly, so L \ ... and U \ ... cost only about n² operations instead of the n³ of a full solve. The determinant falls out for free too: it is the product of U's diagonal, with a sign flip for every row swap (det(P) is ±1).
Cholesky: symmetric positive definite matrices
Many matrices in practice are symmetric positive definite (SPD): covariance matrices, stiffness matrices in mechanics, normal equations A'*A. For them, chol produces an upper-triangular R with A = R'*R, about twice as fast as LU and needing no pivoting. Called with two outputs, [R, p] = chol(A) doesn't throw an error when the matrix isn't SPD; instead p is a positive number. That makes it the standard test for positive definiteness:
K = [4 -2 0; -2 4 -2; 0 -2 4]; % stiffness matrix of a spring chain
[R, p] = chol(K);
fprintf('K is SPD: %d\n', p == 0);
fprintf('R''*R equals K: %d\n', norm(R' * R - K) < 1e-12);
f = [1; 0; 1];
u = R \ (R' \ f); % two triangular solves
fprintf('displacements: %s\n', mat2str(round(u' * 1e4) / 1e4));
M = [1 2; 2 1]; % symmetric, but eigenvalues -1 and 3
[~, p] = chol(M);
fprintf('M is SPD: %d (p = %d)\n', p == 0, p);K is SPD: 1
R'*R equals K: 1
displacements: [0.5 0.5 0.5]
M is SPD: 0 (p = 2)
QR: least squares done carefully
qr writes A as an orthogonal Q (its columns are perpendicular unit vectors, so Q'*Q = I) times an upper-triangular R. The economy form qr(A, 0) keeps only as many columns of Q as A has, which is what you want for tall matrices. For a least-squares problem A*x ≈ b, multiplying by Q' doesn't change lengths, so the problem collapses to the small triangular system R*x = Q'*b:
t = (0:5)';
y = [1.0; 1.8; 3.3; 4.4; 6.1; 7.4];
A = [ones(size(t)) t t.^2]; % fit y = c1 + c2*t + c3*t^2
[Q, R] = qr(A, 0);
fprintf('size of Q: %dx%d, size of R: %dx%d\n', size(Q), size(R));
fprintf('Q''*Q is the identity: %d\n', norm(Q' * Q - eye(3)) < 1e-12);
c = R \ (Q' * y);
fprintf('coefficients via QR: %.4f %.4f %.4f\n', c);
fprintf('coefficients via \\: %.4f %.4f %.4f\n', A \ y);size of Q: 6x3, size of R: 3x3
Q'*Q is the identity: 1
coefficients via QR: 0.9107 1.0196 0.0589
coefficients via \: 0.9107 1.0196 0.0589
This is essentially what backslash does for a rectangular matrix. Avoid the textbook "normal equations" (A'*A) \ (A'*y): forming A'*A squares the condition number and can lose half the available digits on harder problems.
SVD: the most informative factorisation
The singular value decomposition A = U*S*V' exists for every matrix. U and V are orthogonal, and S is diagonal with the singular values σ₁ ≥ σ₂ ≥ … ≥ 0. Singular values are unique (no sign ambiguity) and answer many questions at once:
- the rank is the number of singular values above a small tolerance,
- the 2-norm
norm(A)is σ₁, - the condition number
cond(A)is σ₁ / σₙ, pinv(A), the pseudo-inverse, inverts the non-zero singular values.
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
s = svd(A);
fprintf('singular values: %s\n', mat2str(round(s' * 1e4) / 1e4));
tol = max(size(A)) * eps(s(1));
fprintf('rank from SVD: %d, rank(): %d\n', sum(s > tol), rank(A));
fprintf('norm(A) = %.4f = s(1)\n', norm(A));
[U, S, V] = svd(A);
fprintf('U*S*V'' equals A: %d\n', norm(U * S * V' - A) < 1e-12);singular values: [25.4624 1.2907 0]
rank from SVD: 2, rank(): 2
norm(A) = 25.4624 = s(1)
U*S*V' equals A: 1
The third singular value prints as 0 after rounding; it's really about 10⁻¹⁵, rounding noise standing in for an exact zero: column 3 equals 2·column 2 − column 1, so the matrix only has rank 2.
Low-rank approximation
Keeping only the largest k singular values gives the best possible rank-k approximation of a matrix (the Eckart–Young theorem), and the 2-norm error is exactly the first dropped singular value σₖ₊₁. This is the idea behind image compression and principal component analysis:
[X, Y] = meshgrid(linspace(0, 1, 40));
img = exp(-3 * (X - 0.5).^2 - 5 * (Y - 0.4).^2) + 0.5 ./ (1 + 10 * (X - Y).^2);
[U, S, V] = svd(img);
s = diag(S);
for k = [1 2 4 8]
Ak = U(:, 1:k) * S(1:k, 1:k) * V(:, 1:k)';
fprintf('rank %d: error %.3e, sigma(k+1) %.3e, storage %3d of %d numbers\n', ...
k, norm(img - Ak), s(k + 1), k * (40 + 40 + 1), numel(img));
endrank 1: error 4.888e+00, sigma(k+1) 4.888e+00, storage 81 of 1600 numbers
rank 2: error 2.225e+00, sigma(k+1) 2.225e+00, storage 162 of 1600 numbers
rank 4: error 4.031e-01, sigma(k+1) 4.031e-01, storage 324 of 1600 numbers
rank 8: error 1.333e-02, sigma(k+1) 1.333e-02, storage 648 of 1600 numbers
The error column and the σₖ₊₁ column agree, and they shrink quickly: a rank-8 version of this 40×40 "image" stores 648 numbers instead of 1600 and is off by about 0.04% of the matrix's norm. Real photos behave similarly, because neighbouring pixels are strongly correlated.
Eigen-decomposition
For a square matrix, [V, D] = eig(A) gives A*V = V*D. When A is symmetric, V is orthogonal and A = V*D*V', which turns hard operations into easy ones on the diagonal. Matrix powers are a good example: A^k = V * D^k * V', and the largest eigenvalue dominates the long-run behaviour.
A = [0.9 0.2; 0.1 0.8]; % population moving between two towns
[V, D] = eig(A);
lambda = diag(D);
fprintf('eigenvalues: %s\n', mat2str(round(sort(lambda)' * 1e4) / 1e4));
A20 = V * D^20 / V; % A is not symmetric: use inv(V)
fprintf('A^20 via eig matches A^20: %d\n', norm(A20 - A^20) < 1e-12);
[~, k] = max(lambda);
v = abs(V(:, k)) / sum(abs(V(:, k))); % sign-free, scaled to sum 1
fprintf('long-run split: %.4f / %.4f\n', v);eigenvalues: [0.7 1]
A^20 via eig matches A^20: 1
long-run split: 0.6667 / 0.3333
Eigenvalue 1 belongs to the steady state: in the long run two thirds of the population lives in the first town, whatever the starting split. The other eigenvalue (0.7) tells how fast the system forgets its start.
A rotation has no real eigenvectors, so eig returns complex eigenvalues:
theta = pi / 6;
Rot = [cos(theta) -sin(theta); sin(theta) cos(theta)];
lambda = eig(Rot)
fprintf('|lambda| = %.4f %.4f, angle = %.4f rad\n', abs(lambda), abs(angle(lambda(1))));lambda =
0.8660 + 0.5000i
0.8660 - 0.5000i
|lambda| = 1.0000 1.0000, angle = 0.5236 rad
Condition number: how much can errors grow?
The condition number cond(A) bounds how much a relative error in b can grow in the solution of A*x = b. Roughly, you lose log10(cond(A)) of the 16 significant digits that doubles carry. The Hilbert matrix hilb(n) is the classic bad case:
for n = [4 8 11]
H = hilb(n);
xTrue = ones(n, 1);
b = H * xTrue;
x = H \ b;
fprintf('n = %2d: cond = %.1e, relative error in x = %.0e\n', ...
n, cond(H), norm(x - xTrue) / norm(xTrue));
endn = 4: cond = 1.6e+04, relative error in x = 3e-13
n = 8: cond = 1.5e+10, relative error in x = 3e-07
n = 11: cond = 5.2e+14, relative error in x = 3e-03
The error digits printed here depend on the underlying BLAS/LAPACK library, so MATLAB may show slightly different ones, but the trend is the same everywhere: each factor of 10 in cond costs about one digit, and at n = 12 or 13 (cond near 10¹⁶ and beyond) almost no correct digits are left. MATLAB warns with "Matrix is close to singular or badly scaled" in that situation; take the warning seriously. Use rcond(A) (a cheap estimate of 1/cond) inside loops where computing a full SVD would be too expensive.
Choosing a decomposition
| Situation | Reach for |
|---|---|
| One square system | A \ b (it picks LU, Cholesky or triangular solves for you) |
| Same matrix, many right-hand sides | lu (or chol if SPD) once, then triangular solves |
| Is a symmetric matrix positive definite? | [~, p] = chol(A), p == 0 |
| Overdetermined data fit | A \ b or qr(A, 0) |
| Rank, conditioning, compression | svd |
| Modes, stability, long-run behaviour | eig |
Learn more: the same ideas in Python are in SciPy decompositions; large sparse problems are the topic of Sparse Matrices, and fitting data is expanded in Curve Fitting.
Test yourself
Why factor A with lu once instead of using A \ b for each of 100 right-hand sides?
- LU is more accurate than backslash
- The expensive O(n³) factorisation happens once; each extra solve is only two cheap triangular solves
- Backslash cannot handle many right-hand sides
- LU avoids rounding errors entirely
What does [R, p] = chol(A) returning p > 0 mean?
- A is singular
- A is not symmetric positive definite
- A is orthogonal
- The factorisation used pivoting
You approximate a matrix by its best rank-2 SVD truncation. What is the 2-norm of the error?
- σ₁
- σ₂
- σ₃, the first singular value dropped
- Always zero