Table of Contents
ToggleMatrix addition is the operation where two matrices of the same size are added together. In matrix addition, each element in one matrix is added to the corresponding element in the other matrix.
For matrix addition to be possible, both matrices must have the same dimensions i.e., the same number of rows and columns.
If you have two matrices, say A and B, of the same size, then their sum C is defined as:
C = A + B
Where,
Cij = Aij + Bij
In other words, the element in the ith row and jth column of matrix C is the sum of the corresponding elements in matrices A and B.
Consider the following two matrices:
A = [[1, 2],
[3, 4]]
B = [[5, 6],
[7, 8]]
The sum C = A + B will be calculated as:
C = [[1+5, 2+6],
[3+7, 4+8]]
= [[6, 8],
[10, 12]]
So, the result of adding matrices A and B gives us matrix C:
C = [[6, 8],
[10, 12]]
In NumPy, matrix addition is done using the + operator or using the numpy.add() function. NumPy arrays provide the ability to perform matrix operations element-wise, including addition, which is useful for performing fast mathematical computations.
Following are the key points to remember while performing matrix addition −
+ operator or the numpy.add() function.Before performing matrix addition, let us first create matrices in NumPy. Matrices in NumPy are essentially 2D arrays, and we can create them using the np.array() function as shown below −
# Open Compiler
import numpy as np
# Creating two 2x2 matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Print the matrices
print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
Output:
Following is the output obtained −
Matrix A:
[[1 2]
[3 4]]
Matrix B:
[[5 6]
[7 8]]
The simplest way to add two matrices in NumPy is by using the + operator. This operator will automatically perform element-wise addition of the two matrices.
In the following example, we are adding two matrices “A” and “B” using the “+” operator −
# Open Compiler
import numpy as np
# Creating two 2x2 matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Adding two matrices using the + operator
C = A + B
# Print the result
print("Matrix C (A + B):")
print(C)
Output:
The output obtained is as shown below −
Matrix C (A + B):
[[ 6 8]
[10 12]]
Alternatively, you can perform matrix addition using the numpy.add() function, which works the same way as the + operator. This function takes two matrices (or arrays) as inputs and returns their sum.
In this example, we are adding two matrices “A” and “B” using the “numpy.add()” function −
# Open Compiler
import numpy as np
# Creating two 2x2 matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Adding two matrices using numpy.add() function
C = np.add(A, B)
# Print the result
print("Matrix C (A + B using numpy.add()):")
print(C)
Output:
We get the output as shown below −
Matrix C (A + B using numpy.add()):
[[ 6 8]
[10 12]]
While matrix addition requires matrices of the same shape, NumPy has a powerful feature called broadcasting that allows for element-wise operations between arrays of different shapes.
Broadcasting automatically adjusts the shapes of arrays to allow operations between them. However, for matrix addition specifically, both matrices must have the same shape.
To give you a sense of how broadcasting works (though not directly applicable to matrix addition), here is an example of adding a scalar to a matrix −
# Open Compiler
import numpy as np
# Create a 2x2 matrix
A = np.array([[1, 2], [3, 4]])
# Add a scalar to the matrix using broadcasting
B = A + 10
# Print the result
print("Matrix A + 10:")
print(B)
Output:
The result produced is as follows −
Matrix A + 10:
[[11 12]
[13 14]]
If you try to add two matrices with different shapes (i.e., different dimensions), NumPy will raise an error. This is an important point to watch for when performing matrix addition.
Following is an example of mismatch dimension in NumPy while performing matrix addition −
# Open Compiler import numpy as np # Create two matrices with different shapes A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6, 7]]) C = A + B print(C)
Output:
After executing the above code, we get the following output −
Traceback (most recent call last):
File “/home/cg/root/6734345c5507a/main.py”, line 6, in <module>
C = A + B
ValueError: operands could not be broadcast together with shapes (2,2) (1,3)
Key Takeaway: Learn matrix addition with NumPy efficiently at Vista Academy!
