Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 62 ยท Matrices

Matrices

Learn what a matrix is, how rows, columns and shapes work, and how to build identity, diagonal, triangular and symmetric matrices with NumPy.

A matrix is a rectangular grid of numbers arranged in rows and columns. If a vector is a list, a matrix is a table. You have used matrices all your life without the name: a spreadsheet, a chessboard, a grayscale photo and a timetable are all matrices. In linear algebra we also give them a second life as machines that transform vectors, which is where the real power comes from.

This lesson covers the vocabulary, how to describe a matrix's size, how to read and slice entries in NumPy, and the special matrices you will meet again and again: zero, identity, diagonal, triangular and symmetric. The operations come next in Matrix Addition and Scalar Multiplication and Matrix Multiplication.

Rows, columns and shape

A matrix with m rows and n columns is an m by n matrix (rows first, always). The entry in row i and column j is written A[i][j] or a_ij. Here is a 2 by 3 matrix:

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

It has 2 rows and 3 columns, so its shape is (2, 3). A matrix with the same number of rows and columns is square. A vector is a special matrix with only one column (or one row).

Python Building a matrix and reading its shape

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.

NumPy counts from zero, so the top-left entry is A[0, 0]. In math notation indices usually start at 1, a detail to keep in mind when you translate formulas into code. The slice A[:, 1] means "all rows, column 1".

Anatomy of a matrix

Rows Run left to right. Counted first.
Columns Run top to bottom. Counted second.
Entry One number at row i, column j.
Shape (rows, columns), like (2, 3).

Say "m by n" and always rows first.

1 / 2

Two ways to read a matrix

The same 2 by 3 grid can be seen as two stacks:

  • Stack of rows: two row vectors, (1, 2, 3) and (4, 5, 6). In data science each row is one example.
  • Row of columns: three column vectors, (1, 4), (2, 5) and (3, 6). Each column is one feature.

Switching between these views is the secret to understanding matrix multiplication later. Both are one slice away in NumPy.

Python Rows and columns as vectors

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.

Quick ways to create matrices

You rarely type big matrices by hand. NumPy offers constructors for the usual suspects.

Python Common constructors

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.

reshape changes the shape without changing the numbers, as long as the total count matches. The seed makes the "random" numbers repeatable, which is how all random examples in this track work.

The identity matrix

The identity matrix I is the square matrix with ones on the main diagonal and zeros everywhere else. It plays the role of the number 1: multiplying any matrix or vector by I leaves it unchanged. I is written with a size, such as I_3 for the 3 by 3 case. NumPy builds it with np.eye.

TEXT
I_3 = [ 1  0  0 ]
      [ 0  1  0 ]
      [ 0  0  1 ]
Python The identity leaves things unchanged

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 identity is also the starting point for the inverse matrix: the inverse of A is the matrix that multiplies with A to give I.

Diagonal matrices

A diagonal matrix has non-zero entries only on the main diagonal. They are the simplest matrices to work with: they just scale each coordinate separately. np.diag builds one from a list, or extracts the diagonal of an existing matrix.

Python Diagonal 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.

Diagonal matrices matter because many advanced methods (like diagonalization and the SVD) work by turning a complicated matrix into a diagonal one.

Triangular matrices

An upper triangular matrix has zeros below the diagonal; a lower triangular matrix has zeros above it. They make solving equations easy (you solve from one end), and they are the output of Gaussian elimination and LU decomposition. np.triu and np.tril keep the upper or lower part of a matrix.

Python Upper and lower triangular

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.

Symmetric matrices

A square matrix is symmetric if it equals its own mirror image across the main diagonal: A[i, j] = A[j, i]. Distance tables, covariance matrices and many physical systems are symmetric. A quick test is to compare the matrix with its transpose, A.T, which swaps rows and columns:

Python Testing for symmetry

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.

Symmetry has deep consequences, explored in Transpose and Symmetric Matrices and in the Spectral Theorem.

Always print A.shape when something goes wrong. Most matrix bugs are shape bugs, and knowing the exact (rows, columns) of every object removes half the guesswork.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the shape of a matrix with 4 rows and 7 columns?

    Shapes are always written (rows, columns), so a 4 by 7 matrix has shape (4, 7).
  2. 2. What does A[:, 2] select from a 2D NumPy array?

    The colon means all rows, so the result is the third column (index 2).
  3. 3. What is true about I @ A for the identity matrix I and a compatible matrix A?

    The identity plays the role of the number 1 for matrix multiplication.
  4. 4. Which test checks whether a square matrix S is symmetric?

    A symmetric matrix equals its transpose. Being square alone is not enough.
  5. 5. Which function builds a diagonal matrix with entries 2, 3, 5?

    np.diag with a list creates a matrix with those values on the diagonal and zeros elsewhere.

Summary

  • A matrix is a grid of numbers; an m by n matrix has m rows and n columns, and NumPy reports (rows, columns) in .shape.
  • Entries are read as A[i, j], rows with A[i] and columns with A[:, j]; counting starts at zero.
  • A matrix can be seen as a stack of rows or a row of columns.
  • Constructors: np.zeros, np.ones, np.full, np.arange(...).reshape(...), np.eye and np.diag.
  • Special matrices: identity (ones on the diagonal), diagonal, upper and lower triangular (np.triu, np.tril) and symmetric (A == A.T).

Next up: combine matrices with Matrix Addition and Scalar Multiplication.