Just like you can add or subtract individual numbers, you can perform similar operations on matrices. Matrix addition and subtraction are fundamental operations that work on an element-by-element basis. Think of it as combining or comparing two datasets (represented as matrices) that have the exact same structure.
Before you can add or subtract two matrices, they must satisfy one important condition: they must have the exact same dimensions. This means they must have the same number of rows and the same number of columns.
For example, you can add a matrix to another matrix. However, you cannot add a matrix to a matrix, or a matrix to a matrix. If the shapes don't match, the operation is undefined.
Consider these matrices:
You can calculate because both are matrices. You cannot calculate because is and is . Their dimensions differ.
Matrix addition involves adding the corresponding elements of the two matrices. If you have two matrices, and , with the same dimensions, their sum, , is a matrix where each element is the sum of the elements and .
Mathematically, this is defined as:
Let's add matrices and from our previous example:
Their sum is calculated as:
Each element in the resulting matrix is simply the sum of the elements in the same position in matrices and .
Matrix subtraction follows the same principle as addition: you subtract the corresponding elements. Again, the matrices must have the exact same dimensions.
If , then each element is calculated as:
Using the same matrices and :
Their difference is:
Notice that, unlike regular number subtraction, matrix subtraction is generally not the same as .
Matrix addition shares some familiar properties with scalar addition:
Subtraction, like scalar subtraction, is not commutative ( unless ) and not associative.
NumPy makes matrix addition and subtraction straightforward using the standard + and - operators. NumPy automatically performs the element-wise operations, provided the array shapes (dimensions) are compatible.
First, let's create our example matrices and as NumPy arrays:
import numpy as np
# Define matrix A
A = np.array([[1, 2],
[3, 4]])
# Define matrix B
B = np.array([[5, 6],
[7, 8]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
Now, let's add them:
# Add matrices A and B
C = A + B
print("Matrix C (A + B):\n", C)
This will output:
Matrix C (A + B):
[[ 6 8]
[10 12]]
Similarly, for subtraction:
# Subtract matrix B from A
D = A - B
print("Matrix D (A - B):\n", D)
This will output:
Matrix D (A - B):
[[-4 -4]
[-4 -4]]
What happens if you try to add or subtract matrices with incompatible shapes using NumPy? Let's try adding our matrix to a matrix :
# Define matrix C (different shape)
C_incompatible = np.array([[9, 10, 11],
[12, 13, 14]])
print("Matrix A:\n", A)
print("Matrix C_incompatible:\n", C_incompatible)
try:
result = A + C_incompatible
except ValueError as e:
print("\nError adding A and C_incompatible:", e)
NumPy will raise a ValueError because the shapes don't align for element-wise addition:
Matrix A:
[[1 2]
[3 4]]
Matrix C_incompatible:
[[ 9 10 11]
[12 13 14]]
Error adding A and C_incompatible: operands could not be broadcast together with shapes (2,2) (2,3)
The term "broadcast" refers to a more advanced NumPy feature for handling arrays of different shapes under certain rules, but for basic matrix addition and subtraction, the shapes must match exactly.
Matrix addition and subtraction are relatively simple but form the basis for more complex operations. They might appear when combining or comparing datasets or intermediate results in certain machine learning calculations.
Was this section helpful?
ndarray objects and standard arithmetic operators. Essential for practical application.© 2026 ApX Machine LearningEngineered with