Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 62 ยท Matrices

Matrix Addition and Scalar Multiplication

Learn the simple matrix operations: adding matrices, scaling them, taking the transpose and trace, and the shape rules that decide which operations are allowed.

Once you know what a matrix is, the next question is what you can do with one. The easiest operations work entry by entry: add two matrices, multiply a matrix by a number, subtract one from another. They behave almost exactly like the same operations on vectors, because a matrix is just a grid of numbers that can be added and stretched.

In this lesson you will learn the shape rule that decides when two matrices can be combined, the algebra rules these operations obey, and two small helpers you will use all the time: the transpose and the trace. Matrix multiplication is different and gets its own lesson next.

Four easy matrix operations

Addition Add matching entries. Shapes must be equal.
Scalar multiple Multiply every entry by one number.
Transpose Swap rows and columns.
Trace Sum the main diagonal of a square matrix.

None of these mix rows with columns, so they are cheap and simple.

1 / 2

Adding and subtracting matrices

Two matrices can be added only when they have the same shape. You then add the entries that sit in the same position. If A and B are both 2 by 3, then A + B is a 2 by 3 matrix whose entry in row i, column j is A[i, j] + B[i, j]. Subtraction works the same way.

For example:

TEXT
[1 2 3]   [10 20 30]   [11 22 33]
[4 5 6] + [40 50 60] = [44 55 66]

Notice that the shape never changes. NumPy's + and - already do exactly this for two arrays of equal shape.

Python Adding and subtracting matrices

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.

Scalar multiplication

In linear algebra a plain number is called a scalar. Multiplying a matrix by a scalar c multiplies every entry by c. It scales the whole matrix without changing its shape. A scalar of 0 gives the zero matrix, and a scalar of -1 flips every sign.

TEXT
    [1 2]   [3 6]
3 * [3 4] = [9 12]

Dividing by a number is the same as multiplying by its reciprocal, so A / 2 is 0.5 * A.

Python Scaling a 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.

The algebra rules

Because the operations act entry by entry, they inherit the rules of ordinary numbers. For matrices A, B, C of the same shape and scalars c, d:

  • A + B = B + A (addition commutes)
  • (A + B) + C = A + (B + C)
  • A + 0 = A, where 0 is the zero matrix of the same shape
  • c(A + B) = cA + cB
  • (c + d)A = cA + dA
  • c(dA) = (cd)A

A set of objects with these rules is the starting point of the idea of a vector space: matrices of a given shape form one, just like vectors do. Multiplying two matrices is not on this list, because it does not follow the "commutes" rule. See Matrix Multiplication.

Python Checking the rules numerically

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.

Transpose

The transpose of a matrix A, written A^T, turns rows into columns. If A is m by n, then A^T is n by m, and A^T[j, i] = A[i, j]. The first row of A becomes the first column of A^T.

TEXT
    [1 2 3]          [1 4]
A = [4 5 6]   A^T =  [2 5]
                     [3 6]

Transposing twice returns the original, (A^T)^T = A. The transpose also respects the operations from this lesson: (A + B)^T = A^T + B^T and (cA)^T = cA^T. In NumPy use A.T, which is a cheap view and copies nothing.

A 1D NumPy array has only one axis, so .T does nothing to it. To get a true column vector you need a 2D shape such as (3, 1); see Vectors for the shape conventions this track uses.

Python Transpose and shapes

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.

Trace

The trace of a square matrix is the sum of the entries on its main diagonal, from top-left to bottom-right. It is written tr(A). For

TEXT
[2 9 1]
[0 5 4]   the trace is 2 + 5 + 8 = 15.
[7 3 8]

The trace is only defined for square matrices. It is linear: tr(A + B) = tr(A) + tr(B) and tr(cA) = c * tr(A). Transposing does not change it, because the diagonal stays put. Later you will see that the trace also equals the sum of the eigenvalues, which makes it a handy checksum.

Python Trace with NumPy

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.

Shape rules and a warning about NumPy

Entry-by-entry operations need equal shapes. If they differ, NumPy raises a ValueError... unless the shapes can be broadcast. Broadcasting is a convenient NumPy feature, but it goes beyond the mathematics: strictly speaking, adding a 1D array of length 3 to a 2 by 3 matrix is not a defined matrix operation. It is useful, but when you read formulas, remember that the textbook rule is "same shape only".

Python When shapes do not match

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pair of matrices can be added?

    Addition requires identical shapes, so the entries can be paired by position. Having the same number of entries is not enough.
  2. 2. What is the trace of the matrix with rows [1, 2] and [3, 4]?

    The trace adds the main diagonal entries: 1 + 4 = 5.
  3. 3. If A has shape (4, 7), what shape does its transpose have?

    Transposing swaps rows and columns, so an m by n matrix becomes n by m.
  4. 4. Which statement about matrix addition is true?

    Addition is done entry by entry on ordinary numbers, so it is commutative.

Summary

  • Matrices of the same shape are added or subtracted entry by entry; the shape does not change.
  • A scalar multiple multiplies every entry by one number.
  • These operations obey the usual rules: commutative, associative and distributive.
  • The transpose A^T swaps rows and columns, and A.T in NumPy is a view.
  • The trace is the sum of the diagonal of a square matrix and is linear.
  • Different shapes are an error in math; NumPy broadcasting is an extra convenience.

Next: combine matrices in a new way with Matrix Multiplication.