3.1 - Matrices
(Two-Dimensional Arrays)

Have you heard of The Matrix ? If you haven't watched it, you should.

Two-dimensional arrays of data are also known as a matrix. They can be written in several ways but most people think of them like a typical x-y plane of rows and columns.

Games, grids, and data science use matrices to control, manipulate, and calculate large amounts of data. Whenever you see a shape drawn on a computer, there is a good chance the data for that visualization is being held in a matrix.

A matrix is always listed as height first, then width. So a m x n matrix has height m and width n.

  • A QR Code is a good example of using a 2D Matrix (array) to visualize data. Here's a QR Code Generator to see it in action. And here's a description of the complicated algorithm that generates QR's.

  • A path or maze from a start point to an end is another good example.

  • An extension to a path is a heat map. A heat map is a path that shows different colours based on how many times a particular point is hit.

  • We can also store numbers based on place-value and search much faster this way.

  • Think about checkers, chess, minesweeper, connect-four, battleship...

Here is how to multiply two matrices.

See your task in Repl.it for the assignment.

Aside: A 3-dimensional matrix can be used to store even more data, like depth. Your expensive graphics cards use tons of very large matrices and complex data manipulation to move and display things realistically. If you have an interest in game programming, you will take many classes on matrix manipulation in University. Thankfully, many toolkits already do this for us (like Unity and Unreal Engine).