Lesson 3 of 9 · Linear Algebra

Matrices

A matrix is a rectangular table of numbers of size m×n; we look at its types, its addition and multiplication by a number, and how datasets and layer weights are stored in it.

Mathematics for machine learning Difficulty: Beginner Study time: 35 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

A matrix is a rectangular array of numbers in rows and columns. If a vector is one sample, a matrix gathers many samples, or represents a linear transformation that turns one vector into another.

Learning objectives

  • Reading a matrix's shape and elements
  • Recognizing special matrices (square, diagonal, identity, symmetric)
  • Adding matrices and multiplying them by a number

2 Why study this topic?

A dataset of \(m\) samples, each with \(n\) features, is stored in a matrix \(X \in \mathbb{R}^{m\times n}\). The weights of a fully connected (dense) layer are a matrix \(W\). A greyscale image is a matrix of pixel intensities.

3 Theoretical explanation

Definition and notation

We write \(A \in \mathbb{R}^{m\times n}\) for a matrix of \(m\) rows and \(n\) columns, and the element in row \(i\) and column \(j\) is \(a_{ij}\).

\[A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ \vdots & & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\]

Special matrices

  • Square: its number of rows equals its number of columns.
  • Diagonal: its elements off the main diagonal are zero.
  • Identity \(I\): diagonal with ones on the diagonal; it satisfies \(AI = IA = A\).
  • Symmetric: equal to its transpose, \(A = A^\top\), like the covariance matrix.
  • Zero: all its elements are zero.

Addition and multiplication by a number

Two matrices of the same shape are added element by element, and multiplying by a scalar multiplies every element by that number. These two operations make matrices of the same shape a vector space.

4 Mathematical definitions

Matrix
A rectangular table of real numbers with m rows and n columns.
\[A = [a_{ij}] \in \mathbb{R}^{m\times n}\]
Identity matrix
A square matrix with ones on the diagonal and zeros elsewhere.
\[I_n = \operatorname{diag}(1,\dots,1)\]
Symmetric matrix
A square matrix equal to its transpose.
\[A = A^\top\]
Matrix
A rectangular array of numbers in m rows and n columns, representing a dataset or a linear transformation.
\[A \in \mathbb{R}^{m\times n}\]
Identity matrix
A square matrix with ones on the diagonal and zeros elsewhere; it is the identity element of matrix multiplication.
\[AI = IA = A\]

5 The equations

\[(A+B)_{ij} = a_{ij} + b_{ij}\]
Element-wise addition
\[(cA)_{ij} = c\,a_{ij}\]
Multiplication by a scalar

6 Step-by-step worked example

Adding two matrices and multiplying by a number

Problem: Compute \(2A + B\) where \(A = \begin{bmatrix}1 & 0 \\ 2 & -1\end{bmatrix}\) and \(B = \begin{bmatrix}3 & 4 \\ 0 & 5\end{bmatrix}\).

  1. Multiply A by 2: \(2A = \begin{bmatrix}2 & 0 \\ 4 & -2\end{bmatrix}\).
  2. Add B element by element.

Result: \(2A + B = \begin{bmatrix}5 & 4 \\ 4 & 3\end{bmatrix}\)

7 Practical application in AI

  • Data matrix: Each row is a sample and each column a feature; statistics are computed from it and models are trained on it in one go.
  • Images: A 28×28 greyscale image is a matrix of 784 elements, and a colour image is three matrices (a tensor).
  • Layer weights: A layer that maps 4 inputs to 3 outputs has a \(3\times 4\) weight matrix.

8 Interactive lab linked to the lesson

Vector operations (addition, subtraction, dot product, length, distance and cosine similarity), matrix operations (addition, multiplication, transpose, determinant and inverse), eigenvalues and eigenvectors, and singular value decomposition (SVD) with lower-rank reconstruction.

Open the Linear Algebra Lab

9 Practice exercises

Review questions for reflection

  • How would you represent a set of 500 greyscale images of size 28×28 in a single matrix?
  • Why is the covariance matrix always symmetric?

Common mistakes

  • Reading the shape the wrong way round (columns × rows).
  • Adding two matrices of different shapes.
  • Believing that every square matrix is symmetric.

Practice 5 questions from the question bank for this lesson

10 Lesson summary

  • An m×n matrix has m rows and n columns.
  • Addition and multiplication by a number are done element by element.
  • Special matrices (identity, diagonal, symmetric) recur throughout machine learning.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.