Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 · Vectors and Matrices

Matrix Operations

Learn matrix size, addition, scalar multiplication and matrix multiplication step by step, with a from-scratch implementation checked against NumPy.

A matrix is a rectangular grid of numbers arranged in rows and columns. Matrices store data tables, describe systems of equations, and encode transformations such as rotations. This lesson covers the basic arithmetic, especially the multiplication rule that surprises most learners.

Size and notation

A matrix with m rows and n columns has size m × n (rows first). The entry in row i and column j is a_ij.

Text
A = [ 1  2  3 ]      size 2 × 3
    [ 4  5  6 ]

Here a_12 = 2 and a_23 = 6. A matrix with the same number of rows and columns is square. A column vector is an n × 1 matrix.

Addition and scalar multiplication

Add matrices of the same size entry by entry. Scalar multiplication multiplies every entry:

Text
[1 2]   [5 0]   [6 2]         [1 2]   [ 3  6]
[3 4] + [1 7] = [4 11]    3 · [3 4] = [ 9 12]

Addition is commutative (A + B = B + A). Matrices of different sizes cannot be added.

Matrix multiplication

To multiply A (size m × n) by B (size n × p), the inner sizes must match. The result has size m × p. Each entry is a dot product of a row of A with a column of B:

Text
(AB)_ij = (row i of A) · (column j of B)

Example:

Text
A = [1 2]    B = [5 6]
    [3 4]        [7 8]

AB = [1·5+2·7   1·6+2·8]   [19 22]
     [3·5+4·7   3·6+4·8] = [43 50]

Now compute BA:

Text
BA = [5·1+6·3   5·2+6·4]   [23 34]
     [7·1+8·3   7·2+8·4] = [31 46]

AB ≠ BA. Matrix multiplication is not commutative in general. It is associative, (AB)C = A(BC), and distributive, A(B + C) = AB + AC.

The identity matrix I has ones on the diagonal and zeros elsewhere, and AI = IA = A. For 2 × 2: I = [1 0; 0 1].

Python Matrix multiplication from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why the rule looks like this

Think of A as a machine that transforms a vector x into Ax. Then AB means "apply B first, then A". That is why order matters: rotating then stretching is not the same as stretching then rotating. You will see this in the transformations lesson.

A useful way to see the dot-product rule: if x = ⟨x₁, x₂⟩ is written as a column, then

Text
[a b] [x₁]   [a x₁ + b x₂]
[c d] [x₂] = [c x₁ + d x₂]

Each output is a row dotted with the input vector.

Application: a system of equations

The system 2x + y = 5 and x − y = 1 can be written Ax = b:

Text
[2  1] [x]   [5]
[1 -1] [y] = [1]

Solving it later uses the inverse matrix (see Determinants and Inverses).

Python Totals with a price matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Multiplying entry by entry. That is a different operation (the Hadamard product); matrix multiplication uses rows times columns.
  • Forgetting to check that the inner sizes match.
  • Assuming AB = BA, or that AB = 0 forces A = 0 or B = 0.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

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

    Inner sizes (2 and 2) match; the result has the outer sizes, 3 × 4.
  2. 2. What is the (1,2) entry of AB for A = [1 2; 3 4] and B = [5 6; 7 8]?

    Row 1 of A dotted with column 2 of B: 1·6 + 2·8 = 22.
  3. 3. Which statement about matrix multiplication is true in general?

    Multiplication is associative but not commutative.
  4. 4. What is 2A if A = [1 -1; 0 3]?

    Scalar multiplication scales every entry.

Summary

  • An m × n matrix has m rows and n columns.
  • Add matrices of the same size entry by entry; scalar multiples scale every entry.
  • AB needs inner sizes to match and uses row-by-column dot products.
  • Multiplication is associative but not commutative; I is the multiplicative identity.
  • Matrices package linear systems and data computations compactly.

Next: Determinants and Inverses