MML204

Vectors and Matrices

⏱7 min readπŸ‘οΈEye-Care Reading Mode

What is it?

A vector is an ordered list of numbers. Geometrically, it represents an arrow with a direction and a length (magnitude).

A matrix is a 2D rectangular grid of numbers organized into rows and columns. In machine learning, matrices bundle many data points together or represent linear transformations that stretch, rotate, or project data.

Why we need it

Computers cannot look at a picture of a cat or read a patient's medical history directly. They only process numbers.

Linear algebra allows us to turn any real-world object into numbers:

  • A house is represented as a feature vector: [bedrooms,squareΒ feet,age]T[\text{bedrooms}, \text{square feet}, \text{age}]^T.
  • An entire dataset of 10 00010\,000 houses is organized into a matrix of shape 10 000Γ—310\,000 \times 3.
  • An image is a matrix of pixel brightness values (or a 3D tensor with red, green, and blue channels).
  • Neural network weights that transform input features into predictions are stored as matrices.

Linear algebra gives machine learning a unified mathematical language to manipulate millions of numbers simultaneously.


Explained in detail

1. The Two Ways to Think About a Vector

There are two equally helpful ways to view a vector v=[32]\mathbf{v} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}:

  1. Computer scientist's view: An ordered list of features. Here, x1=3x_1 = 3 and x2=2x_2 = 2.
  2. Physicist's view: An arrow starting at the origin (0,0)(0, 0) and pointing to coordinate (3,2)(3, 2).

The length of this arrow is called its magnitude or norm, written βˆ₯vβˆ₯\|\mathbf{v}\|. Using the Pythagorean theorem:

βˆ₯vβˆ₯=32+22=13β‰ˆ3.61\|\mathbf{v}\| = \sqrt{3^2 + 2^2} = \sqrt{13} \approx 3.61

Think of a vector as driving directions: "drive 3 blocks East, then 2 blocks North". No matter where you start, that instruction is the same displacement.

2. Vector Addition: The Parallelogram Rule

When you add two vectors v\mathbf{v} and u\mathbf{u}, you add their matching components:

v+u=[v1v2]+[u1u2]=[v1+u1v2+u2]\mathbf{v} + \mathbf{u} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} + \begin{bmatrix} u_1 \\ u_2 \end{bmatrix} = \begin{bmatrix} v_1 + u_1 \\ v_2 + u_2 \end{bmatrix}

Geometrically, this is "tip-to-tail" addition. Take vector u\mathbf{u}, place its tail at the tip of v\mathbf{v}, and the combined journey from origin to final destination is v+u\mathbf{v} + \mathbf{u}. This forms a parallelogram.

3. Scalar Multiplication: Scaling and Flipping

A single regular number cc is called a scalar. Multiplying a vector by a scalar multiplies every single component:

cv=c[v1v2]=[cv1cv2]c \mathbf{v} = c \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{bmatrix} c v_1 \\ c v_2 \end{bmatrix}
  • If c>1c > 1: the arrow stretches in the same direction.
  • If 0<c<10 < c < 1: the arrow shrinks.
  • If c<0c < 0: the arrow flips 180∘180^\circ and points in the opposite direction.

4. From Vectors to Matrices

When we stack multiple column vectors side by side, we get a matrix:

A=[uv]=[u1v1u2v2]\mathbf{A} = \begin{bmatrix} \mathbf{u} & \mathbf{v} \end{bmatrix} = \begin{bmatrix} u_1 & v_1 \\ u_2 & v_2 \end{bmatrix}

A matrix with mm rows and nn columns has dimension mΓ—nm \times n. In ML:

  • Row ii usually represents the ii-th example (e.g. house #42).
  • Column jj represents the jj-th feature (e.g. square footage across all houses).

Interactive 2D Vectors & Vector Addition

πŸ‘† Drag dot tips
v:
[,]α΅€
β€–vβ€–:3.61
u:
[,]α΅€
β€–uβ€–:3.16
v + u:
[,]α΅€
β€–v + uβ€–:
Presets

Drag any dot on the canvas (blue v, orange u, or green v + u), or click any coordinate / magnitude box in the instrument panel to edit values dynamically.

What to try

  • Drag the blue dot (vector v\mathbf{v}) and orange dot (vector u\mathbf{u}) anywhere around the grid.
  • Notice the green arrow (v+u\mathbf{v} + \mathbf{u}): see how the dashed lines complete the parallelogram.
  • Move the Scalar slider (cc): watch vector v\mathbf{v} grow, shrink, and flip direction when cc turns negative.
  • Check the live values panel to observe how the coordinates and magnitudes update in real time.
  • Click the "Orthogonal (90Β°)" preset to see two perpendicular vectors.
  • Click "Collinear (Parallel)" to see vectors lying along the exact same line.

Notes & Formula

Vector Norm (Euclidean / L2 Length)

βˆ₯vβˆ₯2=βˆ‘i=1nvi2=v12+v22+β‹―+vn2\|\mathbf{v}\|_2 = \sqrt{\sum_{i=1}^{n} v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

Matrix Dimensions

X∈RmΓ—n(mΒ rows,β€…β€ŠnΒ columns)\mathbf{X} \in \mathbb{R}^{m \times n} \quad (m \text{ rows}, \; n \text{ columns})

SymbolMeaning
vA vector (often written in bold as v or with an arrow)
vα΅’The i-th component of vector v
β€–vβ€–Magnitude (length) of the vector
cA scalar (a single real number)
XA matrix (conventionally written in uppercase bold)
m Γ— nMatrix shape: m rows by n columns
vα΅€Transpose of vector v: turns a column into a row
πŸ’‘

In machine learning conventions, standard vectors are almost always column vectors unless transposed ([β‹…]T[\cdot]^T) into row vectors.

⚠️

Common mistake: Adding vectors of different lengths. Vector addition is only defined when both vectors have the exact same number of dimensions.


Quiz

🧩

Ready to test yourself?

12 questions Β· explanations shown after


Teach & Solidify

πŸŽ“
Feynman Teaching Card

Teach This: Vectors and Matrices

Core Intuition

A vector is an arrow representing features or direction in space; a matrix is a grid of vectors that stores entire datasets or transforms space.

Key Concepts to Explain

  • 1Vector addition (u + v) combines displacement tip-to-tail (parallelogram rule).
  • 2Scalar multiplication (c Β· v) stretches, shrinks, or flips direction (when c < 0).
  • 3In ML tabular datasets, row i is an example/sample, while column j is a feature.

⚠️ What Most Students Get Confused About

Confusing vector addition with scalar addition: you can only add vectors that share the exact same number of dimensions!

✍️
Active Recall Practice

Explain Vectors and Matrices in your own words

Imagine you are explaining this idea to a friend or classmate with no math background. Writing it out forces your brain to bridge gaps in your intuition.

Saved locally on your device

Resources & GitHub

🎯

Mastered this concept?

Tick this box when you feel confident explaining it.

πŸ“
Study Notes & Files1 file

Topic Resources

GitHub↗
πŸ“

Vectors_and_Matrices_Summary.md

Markdown Note

567 B Β· Modified Latest

Was this clear?