Matrices: Algebra, Types & Operations
3227 words
16 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Matrices: Algebra, Types & Operations ## 🎯 Learning Objectives After this topic you will be able to: - Identify a matrix by its dimensions $m \times n$ and refer to its entries $a_{ij}$ - Add and subtract matrices of the same size - Multiply a matrix by a scalar and multiply two matrices (when dimensions are comp...

Matrices: Algebra, Types & Operations
🎯 Learning Objectives
After this topic you will be able to:
- Identify a matrix by its dimensions m×n and refer to its entries aij
- Add and subtract matrices of the same size
- Multiply a matrix by a scalar and multiply two matrices (when dimensions are compatible)
- Compute the transpose of a matrix
- Recognise diagonal, triangular, identity, symmetric, and scalar matrices
- Represent a system of linear equations as a matrix equation Ax=b
- Apply the algebraic properties of matrix operations
📋 Prerequisites
- Vectors Introduction (this week, previous topic) — vectors are the building blocks of matrices
- Basic algebra — operations on real numbers
- Matrices organise data in a rectangular grid; they are the natural language of linear algebra
1. What is a Matrix?
1.1 Intuition: A Spreadsheet of Numbers
Think of a matrix as a rectangular spreadsheet. If you run a small business selling three products across four cities, you might arrange your monthly revenue in a table:
| City A | City B | City C | City D | |
|---|---|---|---|---|
| Product 1 | 1200 | 850 | 2100 | 950 |
| Product 2 | 1800 | 920 | 1750 | 1100 |
| Product 3 | 950 | 1050 | 1300 | 780 |
That table is a 3×4 (3 rows, 4 columns) matrix. Each entry records one number — the revenue for a specific product in a specific city.
Matrices are everywhere in data science: datasets (rows = observations, columns = features), images (pixels), neural network weights, and more.
1.2 Formal Definition
>A=a11a21⋮am1a12a22⋮am2……⋱…a1na2n⋮amn>Definition (Matrix). An m×n matrix is a rectangular array of real numbers with m rows and n columns. We write
The entry in row i, column j is aij or Aij. We say A∈Rm×n. Key terminology:
| Term | Meaning | Example |
|---|---|---|
| Dimension | m×n (rows × columns) | 3×4 |
| Square matrix | m=n | 3×3 |
| Row vector | 1×n matrix | [1−25] |
| Column vector | m×1 matrix | 1−25 |
| Zero matrix | All entries are 0 | 0m×n |
| Main diagonal | Entries a11,a22,…,akk | Where row = column |
1.3 Notation Shortcuts
We often write a matrix as:
- A=[aij] — shorthand for "the matrix with entry aij at position (i,j)"
- Am×n — emphasising the dimensions
2. Matrix Operations
2.1 Matrix Addition (and Subtraction)
Two matrices of the same dimensions can be added entry-wise:
>A=[1324],B=[5768]>Example 1: Matrix addition
>A+B=[1+53+72+64+8]=[610812]>
Properties:
- Commutativity: A+B=B+A
- Associativity: (A+B)+C=A+(B+C)
- Identity: A+0=A (zero matrix is the additive identity)
- Inverse: A+(−A)=0
2.2 Scalar Multiplication
Multiply every entry by the scalar c:
>A=[10−24],3A=[30−612]>Example 2: Scalar multiplication
2.3 Matrix Multiplication
This is the most important (and most confusing) operation. Unlike addition, matrix multiplication is not entry-wise.
>cij=k=1∑naikbkj>Definition (Matrix Multiplication). If A is m×n and B is n×p, then the product C=AB is m×p with entries:
>A=[1324],B=[0110]>The (i,j)-entry of C is the dot product of row i of A with column j of B. Critical requirement: The number of columns of A must equal the number of rows of B. (Diagram) Example 3: Matrix multiplication (2×2)
>∗∗Example4:Matrixmultiplication(2×3by3×2)∗∗>>AB &= \begin{bmatrix} 1(0) + 2(1) & 1(1) + 2(0) \\ 3(0) + 4(1) & 3(1) + 4(0) \end{bmatrix} \\ &= \begin{bmatrix} 0 + 2 & 1 + 0 \\ 0 + 4 & 3 + 0 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 4 & 3 \end{bmatrix} \end{aligned}
> > c_{11} &= 1(1) + 0(0) + (-1)(-2) = 1 + 0 + 2 = 3 \\\\ c_{12} &= 1(2) + 0(1) + (-1)(0) = 2 + 0 + 0 = 2 \\\\ c_{21} &= 2(1) + 1(0) + 3(-2) = 2 + 0 - 6 = -4 \\\\ c_{22} &= 2(2) + 1(1) + 3(0) = 4 + 1 + 0 = 5 \\end{aligned}A = \begin{bmatrix} 1 & 0 & -1 \ 2 & 1 & 3 \end{bmatrix}{2 \times 3},\quad B = \begin{bmatrix} 1 & 2 \ 0 & 1 \ -2 & 0 \end{bmatrix}{3 \times 2}
>A=[1201−13],x=2−10>AB = \begin{bmatrix} 3 & 2 \ -4 & 5 \end{bmatrix}_{2 \times 2} Example 5: Matrix × Column VectorThis is the most common case in linear algebra — a matrix multiplying a column vector produces another column vector (a linear combination of the columns).
>Ax=2[12]+(−1)[01]+0[−13]=[24]+[0−1]+[00]=[23]>
Notice: Ax is a linear combination of the columns of A with coefficients from x.
2.4 Properties of Matrix Multiplication
| Property | Formula | Notes |
|---|---|---|
| Associativity | (AB)C=A(BC) | Always true when defined |
| Distributivity | A(B+C)=AB+AC | Left and right distributivity |
| NOT commutative | AB=BA in general | This is a critical difference from numbers! |
| Identity | AI=IA=A | I = identity matrix |
| Zero | A0=0 , 0A=0 | Zero matrix |
>A=[1324],B=[0110]>Example 6: Non-commutativity
>AB=[2413],BA=[3142]>
AB=BA in this case.
3. Special Matrix Types
3.1 Identity Matrix
The n×n identity matrix In has 1's on the main diagonal and 0's elsewhere:
In acts like the number 1 in multiplication: AIn=A and ImA=A for m×n matrix A.
3.2 Diagonal Matrix
A square matrix where all off-diagonal entries are zero:
3.3 Scalar Matrix
A diagonal matrix where all diagonal entries are equal: D=cIn.
3.4 Triangular Matrices
Upper triangular: all entries below the main diagonal are zero. Lower triangular: all entries above the main diagonal are zero.
3.5 Symmetric Matrix
A square matrix A is symmetric if A=AT, i.e. aij=aji for all i,j.
Symmetric matrices appear everywhere: distance matrices, adjacency matrices, covariance matrices, Hessian matrices (Week 11).
3.6 Zero Matrix
All entries are 0. Acts as the additive identity: A+0=A.
4. Transpose of a Matrix
>A=1352463×2,AT=[123456]2×3>Definition (Transpose). The transpose of an m×n matrix A is the n×m matrix AT where (AT)ij=aji (rows become columns). Example 7: Transpose
Properties of transpose:
| Property | Formula |
|---|---|
| Involution | (AT)T=A |
| Addition | (A+B)T=AT+BT |
| Scalar | (cA)T=cAT |
| Product reversal | (AB)T=BTAT |
The product reversal rule is famously easy to remember: "put your socks on before your shoes" ⇒ the transpose reverses the order.
5. Representing Linear Systems as Matrix Equations
A system of m linear equations in n unknowns:
can be written compactly as:
where A is the m×n coefficient matrix, x is the n×1 unknown vector, and b is the m×1 right-hand side vector.
>⎩⎨⎧x+2y−z=43x−y+2z=−1−x+y+z=2⟺13−12−11−121xyz=4−12>Example 8: System → Matrix form
6. Edge Cases & Gotchas
| Situation | What Happens | Why It Matters |
|---|---|---|
| Dimensions mismatch for addition | Operation undefined | Both matrices must be m×n |
| Inner dimensions don't match for multiplication | AB undefined if A is m×n and B is r×p with n=r | Always check: columns of first = rows of second |
| Multiplying by zero matrix | Always gives zero matrix | A⋅0n×p=0m×p |
| ** AB=0 but neither A nor B is zero** | Possible with matrices! | Unlike real numbers, matrix multiplication has zero divisors |
| ** AB=AC does not imply B=C ** | Cancellation fails without invertibility | Matrices have no division in general |
7. Common Pitfalls
❌ Pitfall 1: Assuming commutativity
The most common mistake. AB and BA are usually different, and one may be defined when the other isn't.
❌ Pitfall 2: Confusing multiplication dimensions
For AB, the inner dimensions must match: (m×n)(n×p)=m×p.
❌ Pitfall 3: Forgetting the transpose reversal rule
(AB)T=BTAT, not ATBT. Students often forget the reversal.
8. Formula Summary Table
| Operation | Formula | Dimension |
|---|---|---|
| Addition | (A+B)ij=aij+bij | Same m×n |
| Scalar multiplication | (cA)ij=c⋅aij | Same m×n |
| Matrix multiplication | (AB)ij=∑kaikbkj | m×p (if A is m×n , B is n×p ) |
| Transpose | (AT)ij=aji | n×m |
| Matrix × vector | (Ax)i=∑jaijxj | m×1 |
9. 📝 Practice Questions
>A=[20−13]>Q1: Matrix addition
>B=[−241−3]>,
>A+B=[2+(−2)0+4−1+13+(−3)]=[0400]>. Compute A+B and A−B.Strategy: Add/subtract corresponding entries.Solution:
>A−B=[2−(−2)0−4−1−13−(−3)]=[4−4−26]>
>A=[120−1]>Q2: Matrix multiplication
>B=[301−2]>,
>AB=[1(3)+0(0)2(3)+(−1)(0)1(1)+0(−2)2(1)+(−1)(−2)]=[3614]>. Compute AB and BA.Strategy: Use dot product of rows with columns.Solution:
>BA=[3(1)+1(2)0(1)+(−2)(2)3(0)+1(−1)0(0)+(−2)(−1)]=[5−4−12]>
>A=[1−12003]>Note: AB=BA — matrices do not commute. Q3: Matrix-vector productCompute Ax where
>x=21−1>and
>Ax=2[1−1]+1[20]+(−1)[03]=[2−2]+[20]+[0−3]=[4−5]>.Strategy: Linear combination of columns of A using entries of x.Solution:
>A=[1324]>Q4: Transpose propertiesVerify (AB)T=BTAT for
>B=[0−110]>,
>AB=[−2−413],(AB)T=[−21−43]>.Strategy: Compute both sides independently.Solution:
>AT=[1234],BT=[01−10],BTAT=[−21−43]>
>A=123245356>They match! ✓ Q5: Symmetric matrix checkIs
>⎩⎨⎧2x−3y+z=5x+y−2z=03x+2y+4z=−1>symmetric?Strategy: Check if A=AT (i.e., aij=aji).Solution: a12=2=a21, a13=3=a31, a23=5=a32. Yes, it is symmetric. Q6: System to matrix formWrite the system as Ax=b:
>213−3121−24xyz=50−1>Solution:
>A=1300112−11>Q7: Diagonal matrix multiplicationIf D=diag(2,−1,3) and
>DA=2(1)−1(3)3(0)2(0)−1(1)3(1)2(2)−1(−1)3(1)=2−300−13413>, compute DA and AD.Strategy: D times A scales rows of A; A times D scales columns of A.Solution:
>AD=1(2)3(2)0(2)0(−1)1(−1)1(−1)2(3)−1(3)1(3)=2600−1−16−33>
>A=[1200],B=[0101]>Q8: Zero divisorsFind A,B=0 such that AB=0.Strategy: Try a matrix with a row of zeros, or use projection matrices.Solution:
>AB=[1(0)+0(1)2(0)+0(1)1(0)+0(1)2(0)+0(1)]=[0000]>
>P=[1000]>Both A and B are non-zero, yet their product is zero! Q9: Idempotent matrixA matrix P is idempotent if P2=P. Show that
>P2=[1000][1000]=[1(1)+0(0)0(1)+0(0)1(0)+0(0)0(0)+0(0)]=[1000]=P>is idempotent.Solution:
>A=147258369>Q10: Trace of a matrixThe trace of a square matrix A is tr(A)=∑iaii. Compute tr(A) for
>tr(AB)=a11b11+a12b21+a21b12+a22b22>. Also show tr(AB)=tr(BA) for 2×2 matrices.Solution: tr(A)=1+5+9=15.For A,B 2×2:
>tr(BA)=b11a11+b12a21+b21a12+b22a22>
These are the same four terms in a different order, so tr(AB)=tr(BA). ✓
🔗 Cross-References
- Next topic: Determinants — a single number that captures key properties of a square matrix
- Week 2 (Gaussian Elimination): Row operations transform matrices into simpler forms
- Week 7 (Similarity): Two matrices can represent the same linear transformation in different bases
- BSMA1001 (Maths 1): Matrix methods for solving systems
- BSCS2004 (ML Foundations): Design matrices, feature matrices, weight matrices
- BSCS3004 (Deep Learning): Neural network weights are matrices; backpropagation uses matrix calculus Join Discord Previous1.1 Vectors IntroductionNext1.3 Determinants