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=โa11โa21โโฎam1โโa12โa22โโฎam2โโโฆโฆโฑโฆโa1nโa2nโโฎ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=[13โ24โ],B=[57โ68โ]>Example 1: Matrix addition
>A+B=[1+53+7โ2+64+8โ]=[610โ812โ]>
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โnโaikโbkjโ>Definition (Matrix Multiplication). If A is mรn and B is nรp, then the product C=AB is mรp with entries:
>A=[13โ24โ],B=[01โ10โ]>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=[12โ01โโ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=[13โ24โ],B=[01โ10โ]>Example 6: Non-commutativity
>AB=[24โ13โ],BA=[31โ42โ]>
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 ImโA=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=โ135โ246โโ3ร2โ,AT=[12โ34โ56โ]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โ1โ2โ11โโ121โโโxyzโโ=โ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โ=โkโaikโbkjโ | mรp (if A is mรn , B is nรp ) |
| Transpose | (AT)ijโ=ajiโ | nรm |
| Matrix ร vector | (Ax)iโ=โjโaijโxjโ | mร1 |
9. ๐ Practice Questions
>A=[20โโ13โ]>Q1: Matrix addition
>B=[โ24โ1โ3โ]>,
>A+B=[2+(โ2)0+4โโ1+13+(โ3)โ]=[04โ00โ]>. 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=[12โ0โ1โ]>Q2: Matrix multiplication
>B=[30โ1โ2โ]>,
>AB=[1(3)+0(0)2(3)+(โ1)(0)โ1(1)+0(โ2)2(1)+(โ1)(โ2)โ]=[36โ14โ]>. 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โ1โ20โ03โ]>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=[13โ24โ]>Q4: Transpose propertiesVerify (AB)T=BTAT for
>B=[0โ1โ10โ]>,
>AB=[โ2โ4โ13โ],(AB)T=[โ21โโ43โ]>.Strategy: Compute both sides independently.Solution:
>AT=[12โ34โ],BT=[01โโ10โ],BTAT=[โ21โโ43โ]>
>A=โ123โ245โ356โโ>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โโ312โ1โ24โโโxyzโโ=โ50โ1โโ>Solution:
>A=โ130โ011โ2โ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โ30โ0โ13โ413โโ>, 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)โโ=โ260โ0โ1โ1โ6โ33โโ>
>A=[12โ00โ],B=[01โ01โ]>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)โ]=[00โ00โ]>
>P=[10โ00โ]>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=[10โ00โ][10โ00โ]=[1(1)+0(0)0(1)+0(0)โ1(0)+0(0)0(0)+0(0)โ]=[10โ00โ]=P>is idempotent.Solution:
>A=โ147โ258โ369โโ>Q10: Trace of a matrixThe trace of a square matrix A is tr(A)=โiโaiiโ. Compute tr(A) for
>tr(AB)=a11โb11โ+a12โb21โ+a21โb12โ+a22โb22โ>. Also show tr(AB)=tr(BA) for 2ร2 matrices.Solution: tr(A)=1+5+9=15.For A,B 2ร2:
>tr(BA)=b11โa11โ+b12โa21โ+b21โa12โ+b22โa22โ>
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