Euler Hall — free maths guides

Check the inner dimensions before multiplying two matrices

Decide whether AB exists by comparing the columns of A with the rows of B. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

The rule, and why it is that rule

Two matrices multiply only when the columns of the first equal the rows of the second:

(m×n) (n×p)  ⟶  (m×p).(m\times n)\,(n\times p)\;\longrightarrow\;(m\times p).

Write the sizes side by side and cancel the middle. The inner pair has to match, and the outer pair survives.

Why: every entry ofABAB is one row ofAA dotted with one column ofBB. A dot product pairs up two lists entry by entry, so the two lists must be the same length. A row ofAA hasnn entries and a column ofBB hasnn entries — the samenn, or there is nothing to pair.

Thatnn appears nowhere in the answer. It is consumed by thenn multiplications each entry needs.

Why the outer pair survives

Look at what the product does to a vector. IfAA ism×nm\times n andBB isn×pn\times p, thenBB eats app-vector and produces annn-vector, and thenAA eats thatnn-vector and produces anmm-vector. The composition goes frompp entries tomm entries, so the combined matrix ism×pm\times p.

The intermediatenn is the thing that has to match:BB's output length must equalAA's input length, or the chaining is meaningless. That is the inner pair.

This is also why the answer keeps the outer pair: the composition starts whereBB starts (widthpp) and ends whereAA ends (heightmm).

Worked example, in the shape the exam asks

Question.AA is4×34\times3,BB is3×23\times2,CC is2×52\times5. Which ofABAB,BCBC,ACAC,ABCABC exist, and what are their sizes?

Step 1 —ABAB.(4×3)(3×2)(4\times3)(3\times2): inner pair33 and33, they match, so it exists and is4×24\times2.

Step 2 —BCBC.(3×2)(2×5)(3\times2)(2\times5): inner pair22 and22, exists,3×53\times5.

Step 3 —ACAC.(4×3)(2×5)(4\times3)(2\times5): inner pair33 and22, do not match, soACAC does not exist.

Step 4 —ABCABC. Do it in two steps. WithABAB first:ABAB is4×24\times2, and(4×2)(2×5)=4×5(4\times2)(2\times5)=4\times5, exists. WithBCBC first:BCBC is3×53\times5, and(4×3)(3×5)=4×5(4\times3)(3\times5)=4\times5, also exists. Both routes give the same4×54\times5 matrix, which is associativity.

Answer.ABAB is4×24\times2,BCBC is3×53\times5,ACAC does not exist,ABCABC is4×54\times5.

By the end you can
  • Decide whether AB exists by comparing the columns of A with the rows of B
  • Predict the size of AB as the outer pair of dimensions
  • Explain why the inner number disappears from the answer
  • Notice that AB can exist while BA does not
Check yourself

AA is6×26\times2,BB is2×62\times6,CC is6×66\times6. Which ofABAB,BABA,ACAC,ABCABC exist, and what are their sizes?

Answer
  • -ABAB:(6×2)(2×6)=6×6(6\times2)(2\times6)=6\times6 ✓
  • -BABA:(2×6)(6×2)=2×2(2\times6)(6\times2)=2\times2 ✓
  • -ACAC:(6×2)(6×6)(6\times2)(6\times6) — inner pair22 and66, does not exist ✗
  • -ABCABC: takeABAB first, which is6×66\times6, then(6×6)(6×6)=6×6(6\times6)(6\times6)=6\times6 ✓

Notice thatACAC fails whileABCABC succeeds. There is no rule that says a sub-product of an existing product must itself exist — the grouping matters.

Work out where to start — free, no accountAnswer six real questions — free, no account