Matrix multiplication: calculate entries and compose transformations

Learn matrix multiplication through row-column dot products, compatible shapes, and a rotation-and-stretch experiment that shows why transformation order matters.

By 10 min read

What you will learn

  • Check matrix dimensions and calculate a product entry using a row-column dot product.
  • Interpret matrix-vector multiplication as a weighted sum of matrix columns.
  • Compose transformations using column vectors and explain why their order matters.
  • Distinguish matrix products, elementwise multiplication, and translations.

Before you start

Stretch (1, 1) horizontally by a factor of 2, then rotate it 90° counterclockwise: you reach (−1, 2). Reverse the order and you reach (−2, 1). Matrix multiplication records that order as well as the calculation.

You will work through a rectangular product, then use two small matrices to combine transformations. Throughout this lesson, vectors are column vectors. In the two-dimensional experiment, both maps use the same fixed x-y basis.

Check the shapes before multiplying

A matrix's shape lists its rows, then columns. An m × n matrix has m rows and n columns. Its product with an n × p matrix has m rows and p columns:

(m × n)(n × p) → (m × p)

The two inner dimensions must match. Each row from the first matrix must have one entry for each entry in a column from the second matrix.

For example, a 2 × 3 matrix can multiply a 3 × 4 matrix, giving 2 × 4. Reversing these two matrices makes the inner dimensions 4 and 2, so that product is undefined. Shape checks come before arithmetic.

Interactive Linear Algebra develops the shape rule and matrix product. The rule works for rectangular matrices, even though transformation sketches often use square ones.

Calculate one entry at a time

Let C have shape 2 × 3 and D have shape 3 × 2. A semicolon separates rows in this compact notation:

C = [1, 2, 3; 4, 5, 6]
D = [7, 8; 9, 10; 11, 12]

For entry (i, j) of CD, take row i of C and column j of D. Multiply their matching entries, then add. This is the dot product of those two lists.

Product entryCalculationValue
Row 1, column 11 × 7 + 2 × 9 + 3 × 1158
Row 1, column 21 × 8 + 2 × 10 + 3 × 1264
Row 2, column 14 × 7 + 5 × 9 + 6 × 11139
Row 2, column 24 × 8 + 5 × 10 + 6 × 12154

Thus CD = [58, 64; 139, 154], with shape 2 × 2. In index notation, the same rule is:

(CD)ᵢⱼ = Σₖ₌₁ⁿ Cᵢₖ Dₖⱼ

The index k runs across C's row and down D's column. The output keeps i and j. Mathematical row numbers here start at 1; Python list positions start at 0.

Read a matrix through its columns

Matrix-vector multiplication offers a second view. For M = [2, 1; 0, 3], its columns are (2, 0) and (1, 3). Multiplying by v = (4, −1) gives:

M v = 4(2, 0) − 1(1, 3) = (7, −3)

The input coordinates weight the matrix's columns. In particular, M(1, 0) selects its first column, and M(0, 1) selects its second. Those basis vectors tell you how the map acts on the coordinate directions.

This connects matrix action to basis and linear combinations. Applying a matrix to every column of another matrix builds their product column by column.

The rank and null space lesson follows this column view further: which outputs can the columns produce, and which input changes leave the output unchanged?

Compare two transformation orders

The experiment starts with a quarter-turn matrix A and a horizontal stretch B. Both act on the same input v = (1, 1). Follow the intermediate coordinates in the readouts, then compare the final tips.

Interactive experiment

Apply B, then A

Use column vectors: AB v means A(B v). Compare the reverse order and inspect one product entry.

Rotate 90° counterclockwise: (x, y) becomes (−y, x).

Double x while keeping y unchanged.

A

0-1
10

B

20
01

AB

0-1
20

BA

0-2
10
Compare the input and both composition outputsInput v is (1, 1). AB v is (-1, 2). BA v is (-2, 1). Both axes run from -3 to 3. AB ≠ BA: these matrices do not commute. This input gives different final outputs.-303-33xy
○ Input v● AB v, solid line□ BA v, dashed lineAll vectors start at the origin. Coincident tips share a location; the plot rescales to keep every result visible.
Input v
(1, 1)
B v
(2, 1)
A(B v)
(-1, 2)
A v
(-1, 1)
B(A v)
(-2, 1)

AB ≠ BA: these matrices do not commute. This input gives different final outputs.

Matrix equality compares every entry of AB and BA. Matching one input's outputs cannot establish it.

Inspect a row-column calculation

Row 1 of A: (0, -1). Column 1 of B: (2, 0).

(0 × 2) + (-1 × 0) = 0

The filled tip shows AB v, and the square tip shows BA v. The open circle marks the input. The plot adjusts its scale as needed; its tick labels show the current coordinate range.

Use “Product entry” to select a cell of AB. The calculation lists the row of A and column of B that produce it. Changing the vector changes its image, while AB itself depends only on the two matrices.

Try identity, shear, and zero. Set v = (0, 0) while keeping the default matrices: both outputs coincide even though AB and BA remain different matrices.

Apply the rightmost matrix first

Here are the default matrices:

A = [0, −1; 1, 0]
B = [2, 0; 0, 1]

B sends (x, y) to (2x, y). A sends (x, y) to (−y, x), a 90° counterclockwise rotation in the displayed x-right, y-up coordinate system.

With column vectors, AB v = A(B v). The rightmost matrix touches the vector first:

  • B(1, 1) = (2, 1), then A(2, 1) = (−1, 2).
  • A(1, 1) = (−1, 1), then B(−1, 1) = (−2, 1).

Multiplying the matrices gives AB = [0, −1; 2, 0] and BA = [0, −2; 1, 0]. Each combined matrix reproduces its corresponding two-step calculation.

The convention matters. Some applications use row vectors written to the left of their matrices. Read the convention before interpreting transformation order; the Markov chains lesson uses that row-vector form for probability distributions.

Separate order from grouping

Two matrices commute when AB = BA. The rotation and stretch above fail this test. Matrix multiplication is therefore not commutative in general.

Matching outputs for one vector cannot prove equality of two maps. Every linear map sends zero to zero. To compare these small matrices, the widget checks all product entries.

Multiplication is associative whenever the dimensions fit: (AB)C = A(BC). You can change the grouping while preserving the sequence of matrices. This lets you combine a fixed chain once, then apply its product to many inputs.

The identity matrix I has ones on its main diagonal and zeros elsewhere. For an m × n matrix M, IₘM = M and MIₙ = M. The two identity matrices can have different sizes.

A compatible zero matrix makes the product zero. The converse fails: two nonzero matrices can also have a zero product. For example, [1, 0; 0, 0] multiplied by [0, 0; 0, 1] gives zero because the second map produces only coordinates that the first map removes.

Check what a linear map can represent

A linear map preserves vector addition and scalar multiplication. Its matrix therefore fixes the origin: M0 = 0. Interactive Linear Algebra explains these defining properties.

A translation by (3, −1) sends the origin to (3, −1). No 2 × 2 matrix acting directly on (x, y) can represent that translation. Add a separate offset, writing Mv + t, to describe an affine map.

Robotics often combines rotation and translation using homogeneous coordinates. In two dimensions, appending a 1 to each point allows a 3 × 3 matrix to carry the offset. In three dimensions, the corresponding rigid-motion representation uses 4 × 4 matrices.

Modern Robotics introduces homogeneous transformation matrices. Their extra coordinate makes the translation possible; an ordinary linear map on the original coordinates still fixes zero.

The experiment's stretch and shear illustrate linear algebra. They change shape and do not represent rigid motions of a robot part. Keep that physical distinction when building a geometry pipeline.

Choose the intended array operation

For two-dimensional NumPy arrays, A @ B calculates the matrix product. A * B multiplies elements using NumPy's broadcasting rules. NumPy documents matrix multiplication with @ and elementwise multiplication with * separately.

For the default A and B, elementwise multiplication gives a zero 2 × 2 array: every nonzero entry of A meets a zero entry of B. The matrix product AB is [0, −1; 2, 0]. Choosing the operator changes the calculation completely.

NumPy also supports stacks of matrices and special handling for one-dimensional arrays. The tensor lesson explains why axis and shape checks matter as arrays grow. Ordinary Python lists do not implement @; the example below calculates the product explicitly.

Reproduce the products in Python

This standard-library example validates rectangular shapes and their inner dimensions. It checks the same rectangular product and transformation order used above, without installing packages.

def shape(matrix):
    if not matrix or not matrix[0]:
        raise ValueError("Use a nonempty matrix")
    width = len(matrix[0])
    if any(len(row) != width for row in matrix):
        raise ValueError("Rows must have equal length")
    return len(matrix), width

def matmul(left, right):
    rows, inner = shape(left)
    right_rows, columns = shape(right)
    if inner != right_rows:
        raise ValueError("Inner dimensions must match")
    return tuple(tuple(sum(left[i][k] * right[k][j]
                           for k in range(inner))
                       for j in range(columns))
                 for i in range(rows))

def matvec(matrix, vector):
    return tuple(row[0] for row in matmul(matrix, tuple((x,) for x in vector)))

C = ((1, 2, 3), (4, 5, 6))
D = ((7, 8), (9, 10), (11, 12))
print("C D:", matmul(C, D))

A = ((0, -1), (1, 0))
B = ((2, 0), (0, 1))
v = (1, 1)
print("A B:", matmul(A, B))
print("B v:", matvec(B, v))
print("A(B v):", matvec(A, matvec(B, v)))
print("B(A v):", matvec(B, matvec(A, v)))
assert matvec(matmul(A, B), v) == matvec(A, matvec(B, v))

Expected output:

C D: ((58, 64), (139, 154))
A B: ((0, -1), (2, 0))
B v: (2, 1)
A(B v): (-1, 2)
B(A v): (-2, 1)

Try it yourself

Exercise 1. P has shape 3 × 2 and Q has shape 2 × 4. What shape does PQ have? Is QP defined? If row 2 of P is (2, −1) and column 3 of Q is (5, 4), calculate entry (2, 3) of PQ.

Show solution: match the inner dimensions

PQ has shape 3 × 4. QP would require Q's four columns to match P's three rows, so QP is undefined.

The requested entry is 2 × 5 + (−1) × 4 = 6. The product's other entries require the remaining rows and columns.

Exercise 2. Keep A as the quarter-turn and B as the horizontal stretch. Calculate AB v and BA v for v = (0, 1). Would matching outputs at v = (0, 0) prove that the matrices commute?

Show solution: follow both orders

B(0, 1) = (0, 1), then A gives (−1, 0). In the reverse order, A(0, 1) = (−1, 0), then B gives (−2, 0).

Both products send zero to zero, as every linear map must. Agreement on that input cannot establish AB = BA; the different outputs for (0, 1) disprove it.

Continue with eigenvalues and eigenvectors to find directions that a matrix leaves on the same line. The cross product introduces a different multiplication rule for two three-dimensional vectors.

Sources and further study