Table of Contents
Overview
In this chapter you will work with two of the most important matrix operations in MATLAB: matrix addition and matrix multiplication. You have already seen how to create vectors and matrices, so here the focus is on how these operations behave, what rules they follow, and how MATLAB applies them.
Matrix Addition in MATLAB
Matrix addition in MATLAB uses the + operator. You can add two matrices if and only if they have exactly the same size. This means they must have the same number of rows and the same number of columns. When you add matrices, MATLAB adds corresponding elements in the same position.
For example, consider two $2 \times 2$ matrices,
$$
A = \begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix},
\quad
B = \begin{bmatrix}
5 & 6 \\
7 & 8
\end{bmatrix}.
$$
In MATLAB you can write:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B
The result is
$$
C = A + B =
\begin{bmatrix}
1 + 5 & 2 + 6 \\
3 + 7 & 4 + 8
\end{bmatrix}
=
\begin{bmatrix}
6 & 8 \\
10 & 12
\end{bmatrix}.
$$
If you try to add matrices of different sizes, MATLAB produces an error. For instance, if A is $2 \times 2$ and D is $2 \times 3, then A + D` is not defined, and MATLAB will tell you that the matrix dimensions must agree.
You can also add a matrix and a scalar. In that case MATLAB adds the scalar to every element of the matrix. For example:
A = [1 2; 3 4];
s = 10;
B = A + s
Now each element of A is increased by 10, so
$$
B =
\begin{bmatrix}
11 & 12 \\
13 & 14
\end{bmatrix}.
$$
This behavior is often called scalar expansion. The scalar is treated as if it were a matrix of the same size, with all entries equal to that scalar, and then added element by element.
Subtraction works in the same way but with the - operator. For example, A - B subtracts corresponding elements, and A - s subtracts the scalar from every entry of A.
Matrix Multiplication with `*`
Matrix multiplication in linear algebra follows specific rules that are different from simple elementwise multiplication. In MATLAB, matrix multiplication uses the * operator. This operation corresponds to standard linear algebra matrix multiplication.
Suppose you have a matrix $A$ of size $m \times n$ and a matrix $B$ of size $n \times p$. You can multiply them in this order to get a matrix $C$ of size $m \times p$,
$$
C = A B.
$$
The crucial condition is that the number of columns of $A$ must equal the number of rows of $B$. In symbols, if $A$ is $m \times n$ and $B$ is $n \times p$, then $C$ exists and is $m \times p$. MATLAB enforces this rule. If the inner dimensions do not match, MATLAB will produce an error that the inner matrix dimensions must agree.
If
$$
A =
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{bmatrix}
\quad (2 \times 3),
\quad
B =
\begin{bmatrix}
7 & 8 \\
9 & 10 \\
11 & 12
\end{bmatrix}
\quad (3 \times 2),
$$
then A*B is valid. In MATLAB:
A = [1 2 3; 4 5 6];
B = [7 8; 9 10; 11 12];
C = A * B
The result is a $2 \times 2$ matrix,
$$
C = A B =
\begin{bmatrix}
1\cdot 7 + 2\cdot 9 + 3\cdot 11 & 1\cdot 8 + 2\cdot 10 + 3\cdot 12 \\
4\cdot 7 + 5\cdot 9 + 6\cdot 11 & 4\cdot 8 + 5\cdot 10 + 6\cdot 12
\end{bmatrix}
=
\begin{bmatrix}
58 & 64 \\
139 & 154
\end{bmatrix}.
$$
In contrast, BA here is not valid, because $B$ is $3 \times 2$ and $A$ is $2 \times 3$, so BA would require the inner dimensions $2$ and $2$ to match, but in this case the inner dimensions would be $2$ and $3$, which do not match. MATLAB will report an error if you try to compute B*A with these dimensions.
Multiplying by Scalars
Multiplying a matrix by a scalar in MATLAB also uses the * operator. The scalar multiplies every element of the matrix. This is different from matrix multiplication of two non scalar matrices, but MATLAB uses the same operator.
For example:
A = [1 2; 3 4];
k = 3;
B = k * A
The result is
$$
B = 3 A =
\begin{bmatrix}
3 & 6 \\
9 & 12
\end{bmatrix}.
$$
It does not matter if the scalar is on the left or on the right, so A * k gives the same result. In both cases, MATLAB simply multiplies each element of A by k.
Matrix Multiplication and Dimensions
When working with matrix multiplication in MATLAB, it is important to keep track of sizes. MATLAB provides the function size to inspect the number of rows and columns in a matrix. You can use it to check if a multiplication is allowed before you try it.
For example:
A = rand(3,4); % 3-by-4 matrix
B = rand(4,2); % 4-by-2 matrix
size(A)
size(B)
C = A * B; % valid, result is 3-by-2
size(C)
The product A * B is valid because the inner dimensions are both 4, and the result has the outer dimensions 3 and 2.
If you change B so that it has a different number of rows that does not match the number of columns of A, then A B will fail. For example, with B = rand(5,2), you cannot compute A B, since A has 4 columns and B has 5 rows.
When you multiply a column vector by a row vector or the other way around, the same dimension rule applies. For example, if u is a column vector of size $n \times 1$ and v is a row vector of size $1 \times m$, then uv results in an $n \times m$ matrix. On the other hand, vu is a $1 \times 1$ scalar, provided the lengths match.
In MATLAB:
u = [1; 2; 3]; % 3-by-1
v = [4 5 6]; % 1-by-3
M = u * v % 3-by-3 matrix
s = v * u % 1-by-1 scalar
Here, M contains all pairwise products of entries of u and v, and s is the sum $1\cdot 4 + 2\cdot 5 + 3\cdot 6$.
Matrix Multiplication vs Elementwise Multiplication
MATLAB also provides elementwise multiplication, which is different from the matrix multiplication in this chapter. Elementwise multiplication uses the . operator and multiplies individual corresponding elements of two arrays of the same size. Matrix multiplication uses and follows the inner dimension rule described earlier.
Although both operations involve multiplication, they serve different purposes and often give very different results. For example, if A and B are both $2 \times 2$ matrices, AB is a matrix product, while A.B is elementwise.
For instance:
A = [1 2; 3 4];
B = [5 6; 7 8];
C1 = A * B; % matrix multiplication
C2 = A .* B; % elementwise multiplication
Here C1 and C2 will have the same size but their contents will be different. Understanding which operator to use is important for getting correct results in linear algebra and in numerical computations.
Matrix exponentiation has a similar distinction. A^2 performs matrix multiplication A*A, while A.^2 squares each element of A individually.
Order of Operations with Matrices
Matrix multiplication is associative, so if the dimensions allow it, you have
$$
(A B) C = A (B C),
$$
but it is not commutative, which means in general $A B \ne B A$. MATLAB follows the same mathematical rules. The order in which you multiply matrices matters, both for the result and for whether the operation is valid at all.
Scalar multiplication interacts simply with matrix multiplication. If $k$ is a scalar and $A$ and $B$ are matrices for which $A B$ is defined, then
$$
k (A B) = (k A) B = A (k B).
$$
In MATLAB, expressions like kAB or A(kB) follow this idea. You can rearrange the scalar part without changing the final value.
MATLAB also respects the usual arithmetic precedence. Multiplication has higher precedence than addition, so in A B + C the multiplication is done before the addition. You can use parentheses to control the order explicitly, for example (A B) + C or A * (B + C).
Practical Computations with Matrix Products
Matrix multiplication appears in many common linear algebra tasks in MATLAB, such as forming linear combinations, applying transformations, and building systems of equations. For now, focus on becoming comfortable with when you can multiply matrices, how to read sizes, and how to interpret the result.
As an example, consider a matrix of data and a vector of weights. Suppose you have a matrix X with 3 observations and 2 variables,
X = [1 2;
3 4;
5 6]; % 3-by-2
w = [0.1; 0.9]; % 2-by-1
y = X * w
Here y is a 3 by 1 column vector. Each entry of y is a weighted combination of the two columns of X. This is a common pattern in many applications, from statistics to engineering models.
Similarly, if you have a square matrix A and a column vector x, the product A*x represents applying the linear transformation given by A to the vector x. You will see more uses of this in later chapters on solving linear systems and working with eigenvalues.