Find a course

Module 2: Matrix AlgebraLesson 4 of 1214 min

Matrix Operations and Multiplication

Add, scale and multiply matrices, check whether sizes are compatible, and see why AB usually differs from BA while the identity changes nothing.

Step 1 of 13Learn

Matrices as tables of numbers

A matrix is a rectangular array of numbers. A matrix with m rows and n columns is called m × n (read "m by n"). The entry in row i and column j is written aᵢⱼ.

In this course we write small matrices on one line, with rows separated by semicolons. So A = [1 2; 3 4] is the 2 × 2 matrix whose first row is 1, 2 and whose second row is 3, 4.

Matrices show up everywhere data has two indices: a spreadsheet of stores by products, a table of flight distances between cities, the pixel brightness at each row and column of an image. In linear algebra they play two big roles: they store the coefficients of a linear system, and they represent transformations that move vectors around.

The easy operations work entry by entry, just as with vectors:

  • Addition: A + B adds matching entries. Both matrices must have the same size.
  • Scalar multiplication: 3A multiplies every entry by 3.
  • Transpose: Aᵀ turns rows into columns. If A = [1 2 3; 4 5 6] is 2 × 3, then Aᵀ = [1 4; 2 5; 3 6] is 3 × 2.

A square matrix with Aᵀ = A is symmetric; the distance table between cities is a natural example, because the distance from Boston to Denver equals the distance from Denver to Boston. Symmetric matrices have especially nice properties that you'll meet in the eigenvalue module.

Step 2 of 13Diagram

Reading an m × n matrix

Reading an m × n matrixParts diagram: Matrix A (m × n), m rows, n columns, Entry aᵢⱼ, Main diagonal, Square when m = n.Matrix A (m ×n)m rowsn columnsEntry aᵢⱼMain diagonalSquare whenm = n

Tap a shape with to see more.

Matrix A (m × n)
m rows

Horizontal lines of entries; the first number in the size.

n columns

Vertical lines of entries; the second number in the size.

Entry aᵢⱼ

Row i, column j: row first, always.

Main diagonal

Entries a₁₁, a₂₂, …, from top left downward.

Square when m = n

Only square matrices can have inverses and determinants.

Step 3 of 13Practice · Choose one

Matrix A is 3 × 2 and matrix B is 2 × 4. What is the size of AB?

Step 4 of 13Learn

Multiplying matrices: rows times columns

Matrix multiplication is not entry by entry. The entry in row i, column j of AB is the dot product of row i of A with column j of B.

Example. A = [1 2; 3 4] and B = [5 6; 7 8].

  • Row 1 of A with column 1 of B: 1·5 + 2·7 = 19
  • Row 1 with column 2: 1·6 + 2·8 = 22
  • Row 2 with column 1: 3·5 + 4·7 = 43
  • Row 2 with column 2: 3·6 + 4·8 = 50

So AB = [19 22; 43 50].

For the dot products to make sense, each row of A must be as long as each column of B. That's the size rule: an m × n matrix times an n × p matrix gives an m × p matrix. The inner numbers must match; the outer numbers give the result's size.

A special case is a matrix times a vector. If A is m × n and x is a vector with n entries, then Ax is a vector with m entries. Better yet, Ax is a linear combination of the columns of A, with the entries of x as weights:

[1 2; 3 4](5, 6) = 5(1, 3) + 6(2, 4) = (17, 39).

That viewpoint connects matrices to everything from the vectors module. The system Ax = b asks: which combination of A's columns produces b? A whole system of equations collapses into one short equation.

Step 5 of 13Practice · Work it out

Let A = [2 −1; 0 3] and B = [1 4; 5 2]. What is the entry in row 1, column 2 of AB?

Step 6 of 13Try the calculator

2 × 2 matrix multiplication

Each entry of AB is a row of A dotted with a column of B. Swap the roles of A and B in your head and check that BA usually comes out different.

AB row 1, col 1
19
AB row 1, col 2
22
AB row 2, col 1
43
AB row 2, col 2
50
BA row 1, col 1 (for comparison)
23

Step 7 of 13Learn

Order matters, and other surprises

Matrix multiplication obeys some familiar rules and breaks others.

Rules that still hold:

  • Associative: (AB)C = A(BC), so you can drop the parentheses.
  • Distributive: A(B + C) = AB + AC.
  • Identity: the identity matrix I, with 1s on the diagonal and 0s elsewhere, acts like the number 1: AI = IA = A.

Rules that break:

  • Not commutative. In general AB ≠ BA. With A = [1 2; 3 4] and B = [5 6; 7 8], AB = [19 22; 43 50] but BA = [23 34; 31 46]. Sometimes one product doesn't even exist.
  • Zero products. AB = 0 is possible even when neither A nor B is the zero matrix. For example, [1 1; 1 1][1 −1; −1 1] = [0 0; 0 0].
  • No cancellation. AB = AC does not imply B = C.

Why isn't multiplication commutative? Because a matrix represents a transformation, and AB means "do B first, then A." Rotate a book 90° about a vertical axis and then flip it about a horizontal axis, and compare that to the reverse order. The book ends up in different positions.

The transpose of a product reverses the order: (AB)ᵀ = BᵀAᵀ. The same reversal will appear for inverses. It's like putting on socks then shoes: to undo it, you take off the shoes first.

Step 8 of 13Practice · True or false

For any two 2 × 2 matrices A and B, AB = BA.

Step 9 of 13Compare

Numbers vs matrices

AspectReal numbersMatrices
ab = ba?AlwaysUsually not
ab = 0 means a = 0 or b = 0?YesNo: two nonzero matrices can multiply to 0
ab = ac lets you cancel a?Yes, if a ≠ 0Only if A is invertible
(ab)c = a(bc)?YesYes
Identity1I, with 1s on the diagonal

Step 10 of 13Practice · Fill in the blanks

Fill in the blanks

Write the product [1 2; 3 4](2, −1) as a linear combination of columns: 2·(1, 3) + (−1)·(2, 4) = (, ).

Step 11 of 13Note

Step 12 of 13Practice · Match the pairs

Match each matrix product with its size.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. (2 × 3)(3 × 5)
  2. (4 × 1)(1 × 4)
  3. (1 × 4)(4 × 1)
  4. (3 × 3)(3 × 2)

Step 13 of 13Practice · Choose one

Which statement about the identity matrix I is true?