Skip to content
Glacius
Linear algebraConcept reference

Matrix products

A matrix product contains the dot product of each left-hand row with each right-hand column.

On this page 8 sections
  1. Overview
  2. Many inputs through one matrix
  3. Complete the product
  4. Every row must fit every column
  5. Scores for observations
  6. Key takeaway
  7. Sources & further reading
  8. Concept connections

01Many inputs through one matrix#

You already know how a matrix acts on one vector. A matrix product applies the left matrix to each column of the right matrix. Collect the resulting output vectors as columns of the product.

For C=ABC=AB, entry CijC_{ij} is output ii for input column jj. It therefore uses row ii of AA and column jj of BB.

A has rows (1,2) and (3,0). B has rows (2,4) and (1,−1). Highlighted row 1 of A and column 2 of B give entry C12=1×4+2×(−1)=2 in C=AB. Question marks mark other entries not yet computed.A has rows (1,2) and (3,0). B has rows (2,4) and (1,−1). Highlighted row 1 of A and column 2 of B give entry C12=1×4+2×(−1)=2 in C=AB. Question marks mark other entries not yet computed.
Figure 1A has rows (1,2) and (3,0). B has rows (2,4) and (1,−1). Highlighted row 1 of A and column 2 of B give entry C12=1×4+2×(−1)=2 in C=AB. Question marks mark other entries not yet computed.
Link to this figure ↗Download SVGDownload PNG

In the diagram, C12C_{12} pairs row (1,2)(1,2) with column (4,−1)(4,-1). The contributions are 44 and −2-2, so C12=2C_{12}=2. The question marks stand for entries still to compute.

02Complete the product#

Keep AA with rows (1,2),(3,0)(1,2),(3,0) and BB with rows (2,4),(1,−1)(2,4),(1,-1). Column 1 of BB is (2,1)(2,1). Applying AA gives (4,6)(4,6), so that is column 1 of CC.

Column 2 of BB is (4,−1)(4,-1). Applying AA gives (2,12)(2,12). Put both output columns together:

C=(42612)C=\begin{pmatrix}4&2\\6&12\end{pmatrix}
Check your reasoning

A has rows (1,0)(1,0), (2,1)(2,1); B has rows (3,2)(3,2), (−1,0)(-1,0). For C=ABC=AB, compute C21C_{21}.

Show answer and explanation
5

Row 2 dots column 1: 2⋅3+1⋅(−1)=52\cdot3+1\cdot(-1)=5.

03Every row must fit every column#

If AA is m×dm\times d and BB is d×kd\times k, each row-column pair has dd products to add. There are mm left rows and kk right columns, giving mkmk output entries.

Cij=∑r=1dAirBrjC_{ij}=\sum_{r=1}^{d}A_{ir}B_{rj}
Check your reasoning

A is 3×23\times2; B is 2×12\times1. A learner says AB is 2×22\times2. Repair its shape.

  1. A2×22\times2
  2. B3×13\times1
  3. C1×31\times3
Show answer and explanation
3×13\times1

Three A rows and one B column give a 3×13\times1 result.

Matching dimensions make the multiplication possible; they do not justify multiplying corresponding matrix entries. Even for two square matrices, each output entry generally uses an entire row and column.

04Scores for observations#

Let a data matrix XX have rows (1,2),(2,0),(0,1)(1,2),(2,0),(0,1). Let WW have columns (2,1)(2,1) and (−1,3)(-1,3), the weights of two scoring rules. Each entry of XWXW scores one observation under one rule.

XW=(454−213)XW=\begin{pmatrix}4&5\\4&-2\\1&3\end{pmatrix}

For example, row 2 under rule 2 is 2(−1)+0(3)=−22(-1)+0(3)=-2. Changing row 2 of XX affects row 2 of the output. Changing column 2 of WW affects column 2 of the output. Reading those roles is a useful check on an implementation.

Check your reasoning

A has score rows P (1,0)(1,0), Q (2,−1)(2,-1). B has observation columns U (3,2)(3,2), V (1,4)(1,4). Find score Q for observation V.

Show answer and explanation
-2

Dot Q with V: 2−4=−22-4=-2.

Key takeaway

For entry (i,j), dot row i of the left matrix with column j of the right matrix.

  • Compute the entries of a compatible matrix product.

Sources & further reading

  1. [1]
  2. [2]
  3. [3]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Matrix products.” Math behind ML. /learn/la-matmul