Matrices and vectors
What a matrix is, how two of them add and multiply, what a vector's length and direction are, and which directions a matrix only stretches.
21 minutes read
4270 words
Contents26
A vector is a column of numbers. A matrix is a rectangle of them, so a vector is the case of a single column. Adding either works one position at a time. Multiplying two matrices builds each entry of the answer from a whole row and a whole column. That one rule is what the rest of the subject rests on.
Assumed knowledge
Arithmetic. Addition, subtraction and multiplication of ordinary numbers, and what a fraction is.
Nothing else. No page here needs to be read first.
Ground covered
Vectors first. What one is, how two are added, what scaling does to one, the length of one and the unit vector along it. The dot product of two, and the test it gives for a right angle.
Matrices second, as the same idea with more than one column. The shape, how an entry is named, addition and scaling again. Multiplication, which does not work position by position, and the rule deciding which shapes can be multiplied at all. Why the order of a product matters. The identity, the inverse that takes the place of division, and the transpose.
Last, what a square matrix does to the plane and to space. The directions it only stretches, which are its eigenvectors. The reflection built from a single vector, how to apply it without writing it down, and why it keeps length. The rotation, first in the plane and then about an axis, and the family both of them belong to.
A reader who can already say why a 2 by 3 matrix times a 3 by 2 matrix gives a 2 by 2 result can skip the page.
Vectors
A vector is a list of numbers written in a column inside brackets. Its entries are named from the top down, so is the first of them.
A vector of two entries is drawn as an arrow from the origin to the point with those coordinates. One of three entries is an arrow in space. The count of entries is the vector’s dimension. Nothing stops it being larger than three, and only the drawing stops.
Adding and scaling vectors
Entries in matching positions are added, which needs the two vectors to have the same number of them. The answer has that many entries again.
Multiplying a vector by a single ordinary number multiplies every entry by it. A single number is called a scalar in this context, to separate it from the vector.
Drawn as arrows, both rules have a shape. Adding two vectors adds their coordinates. Carrying the second arrow to the tip of the first does exactly that. The carried arrow keeps its length and its direction, and only its starting point moves. The sum runs from the origin to where the carried arrow ends.
Order does not matter here, so and reach the same point. Carrying to the tip of traces the other two sides of the same parallelogram.
Scaling by multiplies both coordinates by , which changes the length of the arrow and leaves its direction alone. A above 1 lengthens it, and a between 0 and 1 shortens it. A negative reverses it, because both coordinates change sign at once.
Every multiple of one vector therefore lies on a single line through the origin. The next section picks out the point on that line at distance 1.
Length of a vector
A column vector of two entries can be drawn as an arrow from the origin to the point with those coordinates. Its length is written , and Pythagoras gives it.
The same formula holds for any number of entries, though only two or three of them can be drawn. It is the modulus of a complex number, written for a list rather than for a pair.
The vector has length , which is 5.
Unit vectors
A vector of length 1 is a unit vector. Dividing any vector by its own length gives one, and the result is written .
Dividing here means scaling by , which is equation (3) with . Every entry is divided by the same number, so the direction is untouched and only the length changes.
Dividing by 5 gives , the short arrow in the figure above. That step is called normalising the vector, and a unit vector is often called a normalised one.
Dot product
Two vectors with the same number of entries can be multiplied to give a single ordinary number. Multiply the entries in matching positions, then add the results.
The answer is one ordinary number, however many entries the two vectors have. It is written with a raised dot, and it is also called the scalar product for that reason.
The same quantity has a geometric value. It is the two lengths multiplied, times the cosine of the angle between the vectors.
Setting gives , because the angle is then zero and its cosine is 1. So equation (4) can also be written .
Orthogonal directions
The cosine of a quarter turn is zero. Two vectors at a right angle therefore have a dot product of zero, and a dot product of zero means a right angle.
The right-hand panel above is such a pair. The vectors and give , which is zero.
Orthogonal is the mathematical word for perpendicular, and it is the one used from here on. A set of unit vectors that are all orthogonal to each other is called orthonormal.
A vector orthogonal to a line, a plane or a surface is called a normal to it. In three dimensions one normal vector fixes a whole plane through the origin. That plane is every vector whose dot product with the normal is zero. The word normal means orthogonal here. Normalising is the separate operation of equation (5), and the two share only their spelling.
Rows and columns
A matrix is a rectangular block of numbers written inside brackets. Its horizontal lines are rows and its vertical lines are columns.
The shape of a matrix is its number of rows and its number of columns, in that order. A matrix with 3 rows and 4 columns is a 3 by 4 matrix, written . The shape is also called its dimensions or its size.
One entry is named by the row and the column it sits in. The entry in row and column of a matrix is written , with the row first. So sits in row 2, column 3.
Two shapes have their own names. A matrix with the same number of rows as columns is square. A matrix with a single column is a column vector, which is the vector of equation (1) written as an by 1 matrix. One with a single row is a row vector.
Adding and scaling matrices
Equations (2) and (3) extend to a rectangle without changing. Two matrices of the same shape are added by adding the entries that sit in the same position, and the answer has that same shape.
Subtraction works the same way, with the entries subtracted rather than added.
Shapes that differ cannot be added at all. There is no answer for a 2 by 3 matrix plus a 3 by 2 matrix, because the positions do not correspond.
Scaling works the same way. Multiplying a matrix by a single number multiplies every entry by it.
The shape is unchanged. Scaling by negates every entry, which is how subtraction can be written as .
Multiplication
Multiplying two matrices does not work position by position. Each entry of the answer is built from a whole row of the left matrix and a whole column of the right one.
Take row of and column of . Both are lists of numbers. Multiply the first of one by the first of the other, then the second by the second, taking each pair in turn. Add the products. The total is the entry in row , column of the answer.
The symbol means add up the terms that follow it, once for each value of . Here counts along the row and down the column at the same time, one step in each per term.
Working the marked entry gives , which is , or 139.
The rule only works when the row and the column are the same length. The number of columns of the left matrix must equal the number of rows of the right one. Those two counts vanish in the answer, and the two that remain are its shape.
So a 2 by 3 matrix times a 3 by 2 matrix gives a 2 by 2 matrix. The same two matrices in the other order give a 3 by 3 matrix instead. When the inner two counts differ, the product does not exist.
Order matters
Swapping the two factors of a product of ordinary numbers changes nothing, because and are both 15. Matrices do not behave that way, and and are usually different matrices.
Both products exist and both are 2 by 2. They disagree in three of their four entries. So the words times and by are not enough on their own, and is read as times on the right. The order is part of the statement.
Matrix times a vector
Equation (4) needs no change when the right-hand factor has a single column. The result is another single column.
Each entry of the answer is one row of the matrix spent against the whole input. That makes it a weighted sum of the inputs, with the row supplying the weights.
Read that way, a matrix is a table of weights. Row says how much each input contributes to output , and a zero says that input contributes nothing to that output.
Identity matrix
One square matrix carries 1 down its main diagonal from the top left and 0 everywhere else. That matrix is the identity, written , or when its size needs saying.
It does to matrices what 1 does to ordinary numbers. Reading it through equation (15) says why. Row has a single 1, in position , so output is input and nothing else.
Division and inverses
There is no division of matrices. Nothing is written , and no rule divides one rectangle of numbers by another.
What takes its place is multiplication by an inverse. The inverse of a square matrix is the matrix that returns the identity when multiplied by , in either order.
Because order matters, dividing by has two meanings, and they are and . Writing would not say which.
For a 2 by 2 matrix the inverse is given by a formula. The number is the determinant, written .
The inverse of has determinant , so it is . Multiplying the two gives .
Not every square matrix has an inverse. A determinant of zero leaves equation (18) dividing by zero, and no inverse exists. The matrix is one such, because its second row is twice its first and its determinant is . A matrix without an inverse is called singular.
Transpose
The transpose of is written . It is the matrix whose rows are the columns of , so the entry at row , column comes from row , column .
Transposing turns an matrix into an one. The 2 by 3 matrix of the multiplication figure becomes a 3 by 2 matrix, with rows , and .
A column vector transposes into a row vector. That gives the dot product of equation (6) a second spelling. A 1 by row times an by 1 column is a 1 by 1 answer, which is one number.
One vector can also stand on both sides of a product, as in . Taken in that order it is by 1 times 1 by , so equation (13) makes it a square matrix rather than a number.
Eigenvalues and eigenvectors
A matrix sends most directions somewhere else. A few directions come back pointing exactly where they went in, longer or shorter but not turned.
Such a direction is an eigenvector of the matrix. The factor it is scaled by is the matching eigenvalue, written .
The condition is there because the vector of all zeros satisfies the equation for every and says nothing.
The matrix drawn there is . Its eigenvalues are 3 and 1, with eigenvectors along and .
An by matrix has at most eigenvalues. An eigenvalue of 1 leaves its direction alone. A negative eigenvalue reverses the direction as well as scaling it. An eigenvalue of 0 flattens its direction to nothing, and a matrix with one is singular, which is the condition of equation (18) seen another way.
Reflection matrices
A column vector times its own transpose, written , is called an outer product. Subtracting twice it from the identity gives a reflection.
The eigenvalues say what does. Along it returns , an eigenvalue of , because is 1. Any orthogonal to has by equation (8), so , an eigenvalue of 1. One direction is reversed and everything orthogonal to it is untouched, which is what a mirror does.
Taking in two dimensions gives a matrix of whole numbers.
That sends to and leaves alone. A matrix of this form is a Householder reflection.
Applying a reflection
The matrix of equation (22) has entries, and multiplying a vector by it the ordinary way takes multiplications. There is no need to build it at all. Regrouping the product moves the brackets one step to the right.
The bracket is a single ordinary number, the dot product of equation (6). The reflection of is that number, doubled, times , subtracted from . One dot product and one scaled subtraction do it, which is multiplications in all, and the only thing stored is . For that is twelve multiplications rather than thirty-six. Moving between two reflections then means moving between two vectors of six entries.
Length and inversion
The length of equals the length of . Squaring equation (24), and writing for the number , gives three terms.
The last step uses . A mirror image is as long as the original.
Applying twice gives back . Multiplying equation (22) by itself and collecting terms gives the identity.
Again the step uses . Reflecting a reflection returns the original, so is its own inverse. The matrix is symmetric as well, so .
Choosing the vector
Any unit vector gives a reflection, so there is one for every direction. The balanced direction gives , where is the matrix whose every entry is 1. Each output is then of its own input, minus of each of the others. That is the most even mixing a reflection can do. The direction along one axis, , gives a matrix with in the first place of its diagonal and 1 in the rest. That one flips a single entry and mixes nothing. Moving from one of those directions to the other moves the matrix from mixing everything to mixing nothing.
Rotation in two dimensions
Turning the plane about the origin by an angle is a matrix. Its two columns are where the two axis directions land, which is equation (15) read backwards.
Both columns have length 1, and their dot product is , which is 0. So the columns are orthonormal by equation (8). A square matrix whose columns are orthonormal is called an orthogonal matrix, and a rotation is one.
Nothing is stretched, because every column is a unit vector. The determinant is 1 rather than , which is what separates a rotation from a reflection: both keep lengths, and only the reflection turns the plane over.
Rotation in three dimensions
An axis is needed here as well as an angle. Turning about the axis leaves the third coordinate untouched and does equation (27) to the first two. The two by two block therefore sits in the top left, and the rest is the identity.
Turning about or about is the same three by three matrix with the block moved to the rows and columns that turn.
All three are orthogonal and all three have determinant 1. Two turns one after another are a single matrix, their product, and the order is part of the statement exactly as equation (14) says. Turning about and then about does not land where turning about and then about does.
Undoing a rotation needs no work at all. Multiplying takes the dot product of every pair of columns, and those are orthonormal, so the answer is the identity of equation (16). The inverse of a rotation is its transpose.
Orthogonal matrices and length
A square matrix with is orthogonal, which is the definition the rotations met above. Reflections satisfy it too, by equation (26). Every such matrix keeps lengths, for the reason equation (25) gave in its own case.
Dot products survive as well, so , and the angle between two vectors comes through unchanged. Take a vector holding several signals. The total energy of those signals is the squared length of the vector, and an orthogonal matrix leaves it alone. That is why matrices of this kind mix the delay lines of a reverb. The mixing shares the sound out among the lines and neither adds energy nor removes it.
Figure sources
Seven of the eight drawings come from one script kept beside this page. One helper draws a matrix as a bracketed grid and tints the cells a figure is about. Another rules the paper for the ones that draw arrows. That is why each group shares a look. The script checks its own arithmetic against NumPy before drawing it, so a wrong entry cannot reach the page. It needs NumPy and Matplotlib.
matrices.py
anim.py writes the animated pair. It emits the SVG itself rather than
going through Matplotlib, because the animation is CSS and Matplotlib has no
way to write any. Every rule in it is scoped to the drawing’s own root, so
nothing it defines reaches the page around it. It needs nothing beyond
Python.
anim.py
uv run --with numpy --with matplotlib python3 matrices.py
python3 anim.py
Further reading
Matrices in full, including the operations this page leaves out.1
Matrix multiplication, with the same rule stated several ways.2
The determinant, and what it measures.3
-
Matrix (mathematics). Wikipedia. Retrieved 31 August 2026. https://en.wikipedia.org/wiki/Matrix_(mathematics) (opens in a new tab) ↩︎
-
Matrix multiplication. Wikipedia. Retrieved 31 August 2026. https://en.wikipedia.org/wiki/Matrix_multiplication (opens in a new tab) ↩︎
-
Determinant. Wikipedia. Retrieved 31 August 2026. https://en.wikipedia.org/wiki/Determinant (opens in a new tab) ↩︎