Index Ai Academy
Foundation · Foundation mathVectors and matrices

Adding, scaling and transposing matrices

About 36 minLooking ahead: some prerequisites aren't done yet

Why this matters

Before the big operation of the next lessons, matrix multiplication, come the easy ones. Adding two matrices, scaling one by a number and flipping one over its diagonal are all done entry by entry, with nothing new to learn except the rules about size. They're also everywhere in ML code: an average picture is an add and a scale, a brightness change is a scale, and a data table that arrives stored the wrong way around is fixed with a transpose. By the end of this lesson you'll do all three on paper and in one short line of NumPy each, and you'll know the shape error NumPy gives when two sizes don't match. The transpose in particular comes back in almost every lesson that follows, from matrix products to the weights of a layer.

  • Add and subtract matrices of the same size, entry by entry
  • Scale a matrix by multiplying every entry by one number
  • Transpose a matrix, turning rows into columns, and predict the new size
  • Average pictures and turn a table the right way around with A + B, 2 * A and A.T, and do both on a batch in PyTorch with X.mean(dim=0) and X.T
Quick check
Have a guess before you start. Each morning two shops record the tents they rent, in the same 2 × 2 table layout. How do you get the combined table?

The math: entry by entry

Purpose. The easy operations first, each done one entry at a time.

Adding and subtracting. Two matrices of the same size add entry by entry:

(1420)+(3−152)=(4372)\begin{pmatrix} 1 & 4 \\ 2 & 0 \end{pmatrix} + \begin{pmatrix} 3 & -1 \\ 5 & 2 \end{pmatrix} = \begin{pmatrix} 4 & 3 \\ 7 & 2 \end{pmatrix}

In plain words: each entry of the answer is the sum of the two entries in the same place.

Subtracting works the same way. Two matrices of different sizes can't be added: some entries would have no partner, just like vectors of different lengths in lesson 48.

Scaling. Multiplying a matrix by a number, a scalar, multiplies every entry: 3 × [[1, 4], [2, 0]] = [[3, 12], [6, 0]].