Every matrix operation starts with a size check. Before you add, multiply or invert matrices, you need to know how many rows and columns each one has. This pair of numbers is the dimension of the matrix. I'll first show how the dimension is written, then read it off three common shapes. We'll finish with the rules that the dimension sets for matrix operations.
- How is the dimension of a matrix written ?
- What are the dimensions of a row vector, a column vector and a square matrix ?
- Why does the dimension matter in matrix operations ?
How is the dimension of a matrix written ?
A matrix is a rectangular array of numbers, so two numbers describe its size completely. The convention fixes the order of the two numbers. Mixing up that order is the most common source of size errors in hand calculation and in code.
The notation below puts the number of rows, m, first and the number of columns, n, second. The two numbers are joined by a multiplication sign, and you read the result as "m by n". A matrix with m rows and n columns is called an m x n matrix.

Figure 1. The dimension m x n. The number of rows comes first and the number of columns comes second.
Rows come before columns : a 2 x 3 matrix has 2 rows and 3 columns. A 3 x 2 matrix holds the same number of entries, 6, but it has a different shape.The entry index follows the same order : aij sits in row i and column j. So i runs from 1 to m, and j runs from 1 to n.The number of entries is m x n : a 3 x 3 matrix holds 9 numbers, and a 1 x 3 row vector holds 3.Software uses the same order : in Matlab, size(A) returns [m n]. In Python, the numpy attribute A.shape returns (m, n).
What are the dimensions of a row vector, a column vector and a square matrix ?
Vectors are matrices too. They are the matrices in which one of the two dimensions equals 1. Let's read the dimension off three shapes that appear on almost every matrix page of this handbook.
Start with a row vector. The three elements a, b and c sit side by side in a single row. So the matrix has 1 row and 3 columns, as the two arrows below spell out.

Figure 2. A row vector. One row and three columns give a 1 x 3 matrix.
The same three elements stacked vertically form a column vector. Now there are 3 rows and only 1 column, so the two numbers of the dimension swap places.

Figure 3. A column vector. Three rows and one column give a 3 x 1 matrix.
A matrix with equal numbers of rows and columns is a square matrix. The example below writes each entry with a double index. The first index of aij gives the row and the second gives the column, so a23 sits in row 2 and column 3.

Figure 4. A square matrix. Three rows and three columns give a 3 x 3 matrix.
The transpose swaps the two numbers : transposing the 1 x 3 row vector gives the 3 x 1 column vector. In general, an m x n matrix A gives an n x m matrix AT.A vector usually means a column vector : when a textbook writes a vector x with n elements, it normally means an n x 1 matrix. A row vector is then written as xT.Some quantities exist only for a square matrix : the determinant, the trace, the eigenvalues and the inverse all need m = n.A 1 x 1 matrix behaves like a scalar : for example, a 1 x 3 row vector times a 3 x 1 column vector gives a 1 x 1 result, which is a single number.
Why does the dimension matter in matrix operations ?
Most matrix operations are defined only for certain combinations of sizes. So the dimension check comes before any arithmetic. The same check also tells you the size of the result before you compute a single entry.
Addition and subtraction work element by element. So both matrices must have the same dimension, and the result keeps that dimension. Multiplication follows a different rule. The product AB exists only when the number of columns of A equals the number of rows of B. If A is m x n and B is n x p, the product AB is m x p. In other words, the two inner numbers must match, and the two outer numbers give the size of the result.
The table below applies this rule to the shapes from the previous section. Look at the first two rows. The same two vectors give a single number in one order and a full 3 x 3 matrix in the other order.
A |
B |
Inner numbers |
AB |
1 x 3 |
3 x 1 |
3 and 3, match |
1 x 1 |
3 x 1 |
1 x 3 |
1 and 1, match |
3 x 3 |
2 x 3 |
3 x 2 |
3 and 3, match |
2 x 2 |
3 x 2 |
2 x 3 |
2 and 2, match |
3 x 3 |
3 x 2 |
3 x 2 |
2 and 3, no match |
not defined |
3 x 3 |
3 x 1 |
3 and 3, match |
3 x 1 |
Check the inner pair first : (m x n)(n x p) is defined, and the result is m x p.Swapping the factors can change the size of the result : for a 2 x 3 matrix A and a 3 x 2 matrix B, AB is 2 x 2 while BA is 3 x 3. So in this case AB and BA cannot even be compared.A matrix times a vector gives a vector : an m x n matrix times an n x 1 vector gives an m x 1 vector. This is how a matrix moves a point to a new point, as on the Affine Mapping page.Element-wise operations need equal sizes : Matlab A.*B and numpy A*B multiply entry by entry. They need the same dimension, unless the tool expands one operand automatically.The entries of the product come from rows and columns : the Multiplication page shows how each entry of AB is computed from a row of A and a column of B.