MathsLinear Algebra

Matrix Multiplication
calculator.

Multiply two matrices, with each entry of AB worked out as a row times a column.

How it's worked out

To multiply A by B, the number of columns of A must equal the number of rows of B. An m by k matrix times a k by p matrix gives an m by p matrix. The two inner sizes must match, and the two outer sizes give the size of the answer.

The row-column rule. The entry in row i and column j of AB is row i of A times column j of B. Multiply the two lists entry by entry and add the products. Interactive Linear Algebra gives the same rule.

cij\displaystyle c_{ij}=ai1b1j+ai2b2j+⋯+aikbkj\displaystyle {}= a_{i1}b_{1j} + a_{i2}b_{2j} + \cdots + a_{ik}b_{kj}

Column by column. The same book defines AB one column at a time: column j of AB is A times column j of B. That view helps when B holds vectors you want to transform, such as points to rotate.

The work. Each entry needs k multiplications and k − 1 additions, and AB has m times p entries. Two 5 by 5 matrices take 125 multiplications and 100 additions.

A quick check. Add up each column of A and each row of B. The first list times the second, entry by entry and summed, equals the total of every entry in AB. A single wrong entry breaks that match, so the steps end with it.

Worked examples

Dana totals sales for two stores

Dana runs two stores that sell three products. Each row of A is a store and each column is a product, with the units sold. Each row of B is a product, with its price and its cost per unit in dollars.

A\displaystyle A=[1046385]\displaystyle {}= \begin{bmatrix} 10 & 4 & 6 \\ 3 & 8 & 5 \end{bmatrix}
B\displaystyle B=[5254423212]\displaystyle {}= \begin{bmatrix} \frac{5}{2} & \frac{5}{4} \\ 4 & 2 \\ \frac{3}{2} & \frac{1}{2} \end{bmatrix}

A is 2 by 3 and B is 3 by 2, so AB is 2 by 2. Row 1 of A times column 1 of B is 10 × 2.50 + 4 × 4 + 6 × 1.50, which is $50.00 of revenue for store 1. Its cost is $23.50. Store 2 brings in $47.00 at a cost of $22.25.

The check holds: the four totals add up to 142.75, and so do the column sums of A times the row sums of B. The product needs 12 multiplications in all.

Open this example in the tool

Turning a point a quarter turn

The matrix below turns the plane 90 degrees counterclockwise about the origin. To move the point (3, 1), write it as a column and multiply.

[0−110][31]\displaystyle \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 3 \\ 1 \end{bmatrix}=[−13]\displaystyle {}= \begin{bmatrix} -1 \\ 3 \end{bmatrix}

The point lands at (−1, 3). Multiply again and it lands at (−3, −1), a half turn from where it began. A 2 by 2 matrix times a 2 by 1 column is a 2 by 1 column, so the answer is a point again.

Open this example in the tool

Swapping the order changes the answer

AB\displaystyle AB=[19224350]\displaystyle {}= \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}
BA\displaystyle BA=[23343146]\displaystyle {}= \begin{bmatrix} 23 & 34 \\ 31 & 46 \end{bmatrix}

Both are 2 by 2, but no entry matches. The top left of AB is 1 × 5 + 2 × 7 = 19, while the top left of BA is 5 × 1 + 6 × 3 = 23. Order matters for almost every pair of matrices, so always multiply in the order the problem gives.

Open this example in the tool

Reading your result

Read the size first. If the inner sizes differ, there is no product. A 2 by 3 matrix cannot be multiplied by another 2 by 3, though it can be multiplied by a 3 by 2 or a 3 by 5.

A row times a column is one number. The 1 by 3 row (1, 2, 3) times the column (4, 5, 6) gives the 1 by 1 matrix 32, the dot product. The other way round, a column times a row gives a full grid: (4, 5) as a column times (1, 2) as a row has rows 4, 8 and 5, 10.

The laws that still hold. (AB)C = A(BC), so a chain of products can be grouped either way. A(B + C) = AB + AC as well. The transpose of AB is the transpose of B times the transpose of A, with the order reversed.

Common mistakes

  • Multiplying entry by entry. AB is not the grid of a₁₁b₁₁, a₁₂b₁₂ and so on. Each entry uses a whole row of A and a whole column of B.
  • Using a column of A. The rule is always row of the first matrix, column of the second. Mixing them up gives the product in the other order.
  • Cancelling a matrix. AB = AC does not mean B = C. Two nonzero matrices can even multiply to zero: the matrix of all 1s times the one with rows 1, −1 and −1, 1 gives rows 0, 0 and 0, 0.
  • Expanding (A + B)² as A² + 2AB + B². The middle is AB + BA, and those two are usually different.

Questions people ask

When the number of columns of the first equals the number of rows of the second. A 2 by 3 times a 3 by 2 works and gives a 2 by 2. A 3 by 2 times a 3 by 2 does not exist.

Sources