I sometimes have trouble remembering mathematical matrix syntax and the order of matrix multi­pli­cation, and I'm sure others do too, so here's a quick summary.

Matrix size and indexing is written row-first: an m-by-n matrix has m rows and n columns. (This is the exact opposite of the x,y notation in computer graphics, where the column x is specified first.) A vector of k elements is represented either as a 1-by-k row matrix or a k-by-1 column matrix.

An example 2-by-3 matrix:


Matrix multi­pli­cation multi­plies an m-by-n matrix with an n-by-p matrix, producing an m-by-p matrix: the second matrix ("matrix B") must have as many rows as the first matrix ("matrix A") has columns, and the result has the same number of rows that matrix A has and the number of columns that B has. (AB has A's rows and B's columns.)

Recall the dot product (or scalar product), that takes in two equal-length vectors and returns one number. If the two vectors (of length k) are v and w, then to get their dot product, we multiply corresponding entries of v and w and sum the resulting products together. Mathematically:


Dot product example:

v =18−25−2
w =162−8−5

To multiply two matrixes (A and B), we take single rows of A and single columns of B, then compute their dot product and put that in the inter­section. Cell i,j of the product matrix AB is calculated with row i of A and column j of B.


A couple of examples, including an illustration of when a column vector is multiplied by a row vector and not vice-versa:

a11a12a11b11 +
a11b12 +
a21a22a21b11 +
a21b12 +
v1v2v3v1w1 + v2w2 + v3w3

In linear algebra, a linear function (or linear map) of n variables is often expressed as Ax, with x being a column vector of n variables and A being an n×n matrix. How this works is quite clear when the multi­plication is carried out, in this example for n=3:

3103x1 + 1x2 + 0
0-250 − 2x2 + 5x
0060 + 0 + 6x3

Geometric rotations are also done with matrix multiplication. For 2d points, rotated around the origin, the following matrix is used (with θ being the rotation angle); it is then multiplied with the column vector of an (x, y) point. The result is a new column vector, with the newly rotated x and y. This is so common that GPUs have in-built hardware for this; it's what they're made for.

cos θ−sin θ
sin θcos θ

2D rotation only has one axis and one angle, but for 3D rotation there are three axes that a point can be rotated around.

0cos θ−sin θ
0sin θcos θ
Rotation around x-axis
Rotation around y-axis
cos θ0sin θ
−sin θ0cos θ
Rotation around z-axis
cos θ−sin θ0
sin θcos θ0

created 2022-11-18, edited 2022-11-18. index.