Let’s say A and B are two mxn matrices. We can add these two matrices and get another matrix C. If the element of ith row and jth column of the matrix A, B, and C are Ai,j, Bi,j, and Ci,j, respectively, then we can say:
Let’s look at an example. Let’s say A and B are two 2×3 matrices with the following elements:
In that case, if C = A + B, then
How to add two matrices using Python NumPy?
We can use the following Python code to add two matrices using NumPy:
import numpy A = numpy.array([[1, 2, 3], [4, 5, 6]]) B = numpy.array([[7, 8, 9], [10, 11, 12]]) C = A + B print("A: \n", A) print("B: \n", B) print("C = A + B: \n", C)
Here, we are creating two matrices A and B using the numpy.array() function. We are then adding the two matrices using “C = A + B” statement.
The output of the mentioned program will be:
A: [[1 2 3] [4 5 6]] B: [[ 7 8 9] [10 11 12]] C = A + B: [[ 8 10 12] [14 16 18]]






0 Comments