Matrix-Vector Multiplication
What you'll learn: How to multiply a matrix by a vector to transform data—a fundamental operation underlying predictions in machine learning.
What Is Matrix-Vector Multiplication?
When you multiply a matrix by a vector, you're applying a linear transformation—essentially reshaping or rotating your data in space. Each row of the matrix performs a dot product with the vector, producing one number in the output vector.
Think of it like a recipe card system: each recipe (matrix row) takes your ingredients (input vector) and combines them with specific weights to create one dish (output element).
The Mechanics
Given a matrix A with dimensions m × n and a vector x with n elements:
- Check dimensions: The number of columns in A must equal the length of x
- Compute each output element: The i-th element of the result equals the dot product of the i-th row of A with x
- Result shape: You get an output vector with m elements
Example
Matrix A (2×3):
[2 1 3]
[0 4 1]
Vector x (3 elements):
[1]
[2]
[3]
Result (2 elements):
- First element: (2×1) + (1×2) + (3×3) = 2 + 2 + 9 = 13
- Second element: (0×1) + (4×2) + (1×3) = 0 + 8 + 3 = 11
Output: [13, 11]
Why It Matters
Every neuron in a neural network performs matrix-vector multiplication! Your input features (vector) get combined using learned weights (matrix rows) to produce predictions. This operation transforms raw data into meaningful outputs.
Key Takeaway: Matrix-vector multiplication applies a linear transformation by taking dot products of each matrix row with the input vector—the dimensions must align (matrix columns = vector length), and the output vector has as many elements as the matrix has rows.