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 2×3 matrix to another 2×3 matrix. However, you cannot add a 2×3 matrix to a 3×2 matrix, or a 2×2 matrix to a 2×3 matrix. If the shapes don't match, the operation is undefined.
Consider these matrices:
A=[1324],B=[5768],C=[91210131114]You can calculate A+B because both are 2×2 matrices. You cannot calculate A+C because A is 2×2 and C is 2×3. Their dimensions differ.
Matrix addition involves adding the corresponding elements of the two matrices. If you have two matrices, A and B, with the same dimensions, their sum, C=A+B, is a matrix where each element Cij is the sum of the elements Aij and Bij.
Mathematically, this is defined as:
Cij=Aij+BijLet's add matrices A and B from our previous example:
A=[1324],B=[5768]Their sum C=A+B is calculated as:
C=[1+53+72+64+8]=[610812]Each element in the resulting matrix C is simply the sum of the elements in the same position in matrices A and B.
Matrix subtraction follows the same principle as addition: you subtract the corresponding elements. Again, the matrices must have the exact same dimensions.
If D=A−B, then each element Dij is calculated as:
Dij=Aij−BijUsing the same matrices A and B:
A=[1324],B=[5768]Their difference D=A−B is:
D=[1−53−72−64−8]=[−4−4−4−4]Notice that, unlike regular number subtraction, matrix subtraction A−B is generally not the same as B−A.
Matrix addition shares some familiar properties with scalar addition:
Subtraction, like scalar subtraction, is not commutative (A−B=B−A unless A=B) 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 A and B 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 2×2 matrix A to a 2×3 matrix C:
# 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.
© 2025 ApX Machine Learning