Matrices, like individual numbers and vectors, can undergo arithmetic operations. The simplest of these are addition and subtraction. These operations are fundamental for tasks where you might need to combine or compare datasets that share the same structure. For instance, if you have two matrices representing sales data from two consecutive months, adding them together gives you the total sales over that period.
Before you can add or subtract two matrices, you must check one important condition: the matrices must have the exact same dimensions. This means they must have the same number of rows and the same number of columns.
If you have a matrix A with dimensions m×n (read as "m by n," meaning m rows and n columns) and a matrix B with dimensions p×q, you can only add or subtract them if m=p and n=q. If their shapes don't align perfectly, the operation is undefined. You can't add an element that doesn't have a corresponding partner in the other matrix.
Once you've confirmed that two matrices have identical dimensions, the process of adding or subtracting them is straightforward. The operation is performed element-wise. This means you take the element in the first row and first column of the first matrix and add it to the element in the first row and first column of the second matrix. You repeat this for every single position.
Let's say we have two matrices, A and B, and we want to find a new matrix C such that C=A+B. The value of any element in C at row i and column j, denoted as Cij, is simply the sum of the elements at the same position in A and B.
Formally, this is written as:
Cij=Aij+BijThe same principle applies to subtraction:
Cij=Aij−BijThe diagram below illustrates this element-wise addition for two 2x2 matrices. Each element in the resulting matrix is the sum of the elements from the corresponding positions in the input matrices.
Each element at position (i,j) in the resulting matrix is the sum of the elements at position (i,j) from the original matrices.
Imagine a small company tracks the number of products sold by two sales agents across three product categories. The sales for January are stored in matrix J, and February's sales are in matrix F.
Here are the sales for January (matrix J):
J=[5075359011060]And the sales for February (matrix F):
F=[45804011010050]Both matrices are 2×3, so we can add them to get the total sales for both months.
Total Sales=J+F=[50+4575+8035+4090+110110+10060+50]=[9515575200210110]Likewise, if we wanted to see the change in sales from January to February, we could subtract J from F:
Change in Sales=F−J=[45−5080−7540−35110−90100−11050−60]=[−55520−10−10]The resulting matrix shows that sales for Agent 1 dropped for products 1 and 3 but increased for product 2. Agent 2 saw an increase in sales for all products.
Matrix addition follows some familiar properties from standard arithmetic, which can be useful to remember:
These properties hold because the underlying operation is just the addition of individual numbers, which are themselves commutative and associative.
In Python with the NumPy library, performing these operations is as simple as using the standard + and - operators. NumPy automatically handles the element-wise calculations.
import numpy as np
# January sales
J = np.array([
[50, 35, 110],
[75, 90, 60]
])
# February sales
F = np.array([
[45, 40, 100],
[80, 110, 50]
])
# Total sales
total_sales = J + F
print("Total Sales (J + F):")
print(total_sales)
# Change in sales
change_in_sales = F - J
print("\nChange in Sales (F - J):")
print(change_in_sales)
If you try to add or subtract matrices with mismatched dimensions in NumPy, it will raise a ValueError, protecting you from performing an invalid operation. This behavior reinforces the mathematical rule in a practical coding environment.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with