This page provides a concise reference and a sequence of practice problems for matrix calculations in base R. It is suitable for review or for use alongside lectures on linear algebra, regression, and numerical computation.
Tip: Use Ctrl/Cmd + Enter in RStudio to run selected lines. Use set.seed() to obtain reproducible random matrices.
Quick Start (R)
Create & Shape
# Create matrices
A <- matrix(1:9, nrow = 3, ncol = 3) # fills by column by default
B <- matrix(seq(2, 20, by = 2), nrow = 3) # another 3x3
C <- matrix(rnorm(12), nrow = 3, ncol = 4) # 3x4 random
# Bind and diag
X <- cbind(1, A) # add intercept column
Y <- rbind(A, 10:12)
I3 <- diag(3) # 3x3 identity
Arithmetic & Algebra
# Elementwise vs matrix multiplication
A + B # addition
A * B # elementwise product
A %*% B # matrix product
# Transpose, inverse, determinant
At <- t(A)
if (det(A) != 0) Ainv <- solve(A)
# Norms and rank
norm_A <- norm(A, type = "F")
rank_A <- qr(A)$rank
Indexing & Apply
A[1, 2] # element (row 1, col 2)
A[ , 2] # column 2 (as vector)
A[1:2, 2:3] # submatrix
A[diag(3) == 1] <- 5 # set diagonal to 5
rowSums(A); colMeans(A)
apply(A, 1, sd) # row-wise sd
Systems, Eigen, SVD
# Linear system Ax = b
set.seed(1)
A <- matrix(rnorm(9), 3, 3)
b <- rnorm(3)
x <- solve(A, b) # solves Ax = b
# Eigen and SVD
E <- eigen(A) # E$values, E$vectors
sv <- svd(A) # U, D, V
# Least squares (normal equations)
X <- cbind(1, matrix(rnorm(20), 10, 2))
y <- rnorm(10)
beta_hat <- solve(t(X) %*% X, t(X) %*% y)
Mini Cheat‑Sheet
matrix(data, nrow, ncol, byrow = FALSE) — build a matrix.
cbind(...), rbind(...) — column/row bind.
diag(n) — identity; diag(v) — diagonal with entries v.
Work through these in order. Each problem includes an optional solution. Unless stated otherwise, assume all matrices are conformable and invertible where needed.
1) Basic construction & products
Create two 3×3 matrices A and B. Compute A + B, A * B (elementwise), and A %*% B. Check whether A %*% B equals B %*% A.
Show solution (R)
set.seed(42)
A <- matrix(sample(1:9), 3, 3)
B <- matrix(sample(1:9), 3, 3)
A + B
A * B
A %*% B
all.equal(A %*% B, B %*% A) # generally FALSE
2) Transpose, determinant, inverse
Compute t(A), det(A), and (if invertible) solve(A). Verify numerically that A %*% solve(A) is the identity.
Show solution (R)
At <- t(A)
DA <- det(A)
if (abs(DA) > 1e-12) {
Ainv <- solve(A)
I3 <- A %*% Ainv
round(I3, 8)
}
3) Indexing & replacement
Replace the diagonal entries of A with 5. Extract the submatrix formed by rows 1–2 and columns 2–3.
Show solution (R)
diag(A) <- 5
subA <- A[1:2, 2:3]
A; subA
4) Row/column summaries
Compute rowSums(A), colMeans(A), and the row‑wise standard deviation using apply.
Show solution (R)
rowSums(A)
colMeans(A)
apply(A, 1, sd)
5) Linear system
Generate a random invertible 3×3 matrix A and vector b. Solve Ax = b for x.
Show solution (R)
set.seed(1)
A <- matrix(rnorm(9), 3, 3); b <- rnorm(3)
x <- solve(A, b)
# check
max(abs(A %*% x - b))
6) Least squares via normal equations
Simulate data with two predictors (plus intercept) and solve for \(\hat{\beta}\) using (X'X)^{-1} X'y.