INDEX: LINEAR-ALGEBRA-FOR-ML / MATH-LA-06READING_TIME: 15 mins

2.3 Transition to Matrices

Up to now, we have written out our equations line-by-line using variables like xx and yy. But in machine learning, we deal with millions of data points and features. Writing out equations by hand is impossible!

To handle large-scale data, we pack our numbers into grids called Matrices. In this lecture, we will learn how to rewrite systems of equations in matrix form (Ax=bA\mathbf{x} = \mathbf{b}) and master the core rules of Matrix Multiplication.


1. Moving from Equations to Matrices (Ax=bA\mathbf{x} = \mathbf{b})

Let's take a standard system of linear equations:

2x1+3x2=122x_1 + 3x_2 = 12 x12x2=1x_1 - 2x_2 = -1

We can separate this system into three compact parts:

  1. AA (Coefficient Matrix): A grid containing only the coefficients (the numbers in front of the variables). A=[2312]A = \begin{bmatrix} 2 & 3 \\ 1 & -2 \end{bmatrix}
  2. x\mathbf{x} (Variable Vector): A column vector of our unknown variables. x=[x1x2]\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}
  3. b\mathbf{b} (Target Vector): A column vector containing the answers on the right side of the equals sign. b=[121]\mathbf{b} = \begin{bmatrix} 12 \\ -1 \end{bmatrix}

Now, we can write the entire system of equations in a single, elegant line of matrix algebra:

Ax=bA\mathbf{x} = \mathbf{b}


2. Matrix-Vector Multiplication: How It Works

To make sure Ax=bA\mathbf{x} = \mathbf{b} is actually the same as our original equations, we need to know how to multiply a matrix by a vector.

The Rule: Row Dot Column

To multiply a matrix by a vector, we take each row of the matrix and compute its dot product with the column vector.

[2312][x1x2]=[(2×x1)+(3×x2)(1×x1)+(2×x2)]=[2x1+3x2x12x2]\begin{bmatrix} 2 & 3 \\ 1 & -2 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} (2 \times x_1) + (3 \times x_2) \\ (1 \times x_1) + (-2 \times x_2) \end{bmatrix} = \begin{bmatrix} 2x_1 + 3x_2 \\ x_1 - 2x_2 \end{bmatrix}

This yields a new column vector. Matching this result to our target vector b=[121]\mathbf{b} = \begin{bmatrix} 12 \\ -1 \end{bmatrix} gives us our original equations back!


3. Matrix-Matrix Multiplication

In machine learning, we often multiply matrices by other matrices (for example, when passing a batch of inputs through a neural network layer).

Suppose we want to multiply matrix AA (size 2×22 \times 2) by matrix BB (size 2×22 \times 2) to get a new matrix C=ABC = AB:

A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

We calculate each cell in the result matrix CC using the Row-by-Column rule:

  • Row 1 of A ×\times Column 1 of B     \implies Cell C11C_{11}: (1×5)+(2×7)=5+14=19(1 \times 5) + (2 \times 7) = 5 + 14 = 19
  • Row 1 of A ×\times Column 2 of B     \implies Cell C12C_{12}: (1×6)+(2×8)=6+16=22(1 \times 6) + (2 \times 8) = 6 + 16 = 22
  • Row 2 of A ×\times Column 1 of B     \implies Cell C21C_{21}: (3×5)+(4×7)=15+28=43(3 \times 5) + (4 \times 7) = 15 + 28 = 43
  • Row 2 of A ×\times Column 2 of B     \implies Cell C22C_{22}: (3×6)+(4×8)=18+32=50(3 \times 6) + (4 \times 8) = 18 + 32 = 50

So, our final result is: AB=[19224350]AB = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}

[!WARNING] Order Matters! In regular math, 2×3=3×22 \times 3 = 3 \times 2. But in matrix multiplication, ABBAAB \neq BA in general! Changing the order of multiplication will yield a completely different result, or might even be mathematically impossible because the sizes don't match.


4. Visualizing Matrices in Code

In machine learning libraries like NumPy or PyTorch, matrices are represented as 2D arrays. Below is a visualization of a coefficients matrix in grid form:

MATRIX_COEFFICIENT_GRID
NON-SINGULARDET: -7 (NON-ZERO)
2
3
1
-2

5. Master the Calculation in the Lab

To build your mechanical intuition of how rows dot-product with columns step-by-step, go to the computational sandbox:

🔬 Matrix Multiplication & Vector Space Visualizer


6. Check Your Understanding

Quiz / Test Your Knowledge

If you multiply matrix A of size (3 x 2) by matrix B of size (2 x 4), what will be the size of the resulting matrix C?

🔬 Interactive Laboratory Sandbox

Run practical simulations and numerical verifications associated with the mathematical equations derived in this note: