1.1.5Linear Algebra Essentials

Matrix multiplication rules and dimensionality

1,754 words8 min readdifficulty · medium

What is matrix multiplication?

WHAT the shapes tell you:

Am×n  Bn×p  =  Cm×p\underbrace{A}_{m\times n}\;\underbrace{B}_{n\times p}\;=\;\underbrace{C}_{m\times p}

The two inner numbers (nn and nn) must match and get "consumed"; the two outer numbers (mm and pp) survive to become the shape of the answer.

Figure — Matrix multiplication rules and dimensionality

Deriving the rule from first principles

We don't just state cij=kaikbkjc_{ij}=\sum_k a_{ik}b_{kj}. We discover it.

Why the dimensions must match: bj\mathbf{b}_j lives in Rn\mathbb{R}^n (it has nn entries, one per row of BB). For AA to eat an nn-vector, AA must have exactly nn columns. That is the entire reason for the "columns of AA = rows of BB" rule. It is a plug fits socket condition.


Key algebraic rules (and WHY)

Property Holds? Why
Associative (AB)C=A(BC)(AB)C = A(BC) Composition of functions is associative
Distributive A(B+C)=AB+ACA(B+C)=AB+AC Linearity of the transformation
Commutative AB=BAAB = BA ❌ (generally) "Rotate then stretch" ≠ "stretch then rotate"
Identity AI=IA=AAI=IA=A II = "do nothing" transform
Transpose (AB)=BA(AB)^\top=B^\top A^\top Order reverses (proof below)

Worked examples


Common mistakes


Active recall

Recall Test yourself (hide answers)
  • What condition allows ABAB to exist? → columns of AA = rows of BB.
  • Shape of (m×n)(n×p)(m\times n)(n\times p)? → m×pm\times p.
  • Formula for cijc_{ij}? → kaikbkj\sum_k a_{ik}b_{kj}.
  • Is matrix mult commutative? → No (in general).
  • What is (AB)(AB)^\top? → BAB^\top A^\top.
Recall Feynman: explain to a 12-year-old

Imagine two vending machines lined up. The first machine takes 4 coins and spits out 5 snacks. The second machine takes exactly 5 snacks and spits out 2 drinks. They only connect because the first's output (5) matches the second's input (5). The combined machine takes 4 coins, gives 2 drinks — the 5 in the middle "disappears" inside. To find each output, you mix a whole row of one recipe with a whole column of the other, multiplying pairs and adding them up.


Connections

When is the product ABAB defined?
When the number of columns of AA equals the number of rows of BB.
What is the shape of (m×n)(n×p)(m\times n)(n\times p)?
m×pm\times p (inner dims match and cancel, outer dims survive).
Give the formula for entry cijc_{ij} of ABAB.
cij=k=1naikbkjc_{ij}=\sum_{k=1}^{n} a_{ik}b_{kj} = row ii of AA dotted with column jj of BB.
Is matrix multiplication commutative?
No; ABBAAB\ne BA in general (they may even have different shapes).
Why must columns of AA equal rows of BB?
Because AA must act on each column of BB, which lives in Rn\mathbb{R}^n; AA needs exactly nn columns to accept an nn-vector.
What is (AB)(AB)^\top?
BAB^\top A^\top — the order reverses.
How is matrix mult different from the Hadamard product?
Matrix mult uses row·column dot products (composition of maps); Hadamard \odot is element-wise.
In a layer y=Wx\mathbf{y}=W\mathbf{x} with WW being 3×43\times4, what shape is x\mathbf{x} and y\mathbf{y}?
x\mathbf{x} is 4×14\times1, y\mathbf{y} is 3×13\times1.
Column view: column jj of ABAB equals?
AA times column jj of BB.
Is matrix multiplication associative?
Yes, (AB)C=A(BC)(AB)C=A(BC), because composition of linear maps is associative.

Concept Map

composition

output must match input

plug fits socket

derives

inner n consumed

function composition

rotate vs stretch differ

do-nothing map

order reverses

from

Matrix as transformation machine

Chaining AB means do B then A

Dimension rule: cols A = rows B

Linearity: A acts on columns

Entry c_ij = sum a_ik b_kj

Shape m x n times n x p = m x p

Associative

Not commutative

Transpose flips order

Identity does nothing

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, matrix multiplication ko samajhne ka sabse easy tarika hai: har matrix ek machine hai jo vectors ko transform karti hai. Jab hum A×BA \times B karte hain, matlab pehle BB wali transformation lagao, phir uske output pe AA wali transformation lagao. Isliye rule aata hai — AA ke columns aur BB ke rows barabar hone chahiye. Kyun? Kyunki BB ka har column ek nn-dimension ka vector hai, aur AA ko us nn-vector ko "khaana" hai, to AA ke paas exactly nn columns hone hi chahiye. Yahi hai "plug fits socket" wali baat.

Shape yaad rakhne ka mantra: "Inners match, outers survive." Agar AA hai m×nm\times n aur BB hai n×pn\times p, to beech wale nn aur nn match karke gayab ho jaate hain, aur bahar wale mm aur pp bach jaate hain — result banta hai m×pm\times p. Har ek entry cijc_{ij} nikaalne ke liye AA ki ii-th row ko BB ki jj-th column ke saath dot product karo (pairs multiply karo, phir add karo).

Ek important cheez: matrix multiplication commutative nahi hai, yaani ABAB aur BABA generally alag hote hain — kabhi to inki shape hi alag hoti hai! Jaise "pehle mozey pehno phir joote" vs "pehle joote phir mozey" — order matters. Aur ek common galti: matrices ko element-by-element multiply mat karo (woh Hadamard product hai, alag cheez). Matrix mult hamesha row-column dot product se hota hai.

ML mein yeh core hai bhai — neural network ka har layer y=Wx\mathbf{y}=W\mathbf{x} ek matrix-vector multiplication hi to hai, aur layers stack karna matrices ko multiply karke compose karna hai. Isliye dimensionality ka hisaab bilkul solid hona chahiye, warna "shape mismatch" error har jagah aayega.

Test yourself — Linear Algebra Essentials

Connections