Adding two matrices - Two dimensional ndarray objects
- For adding two matrixes together both the matrices should have equal number of rows and columns.
- To add two matrices, the corresponding elements of each matrix are added and placed in the same position in the resultant matrix.
- The result of the matrix addition is a matrix of the same number of rows and columns.
- The ndarray of NumPy module supports matrix addition through the method __add__() which adds two ndarray objects of the same shape and returns the sum as another ndarray object.
Example
# import numpy module
import numpy as np
# import python standard library module - random
import random
# Routine for printing a 2x2 matrix
def PrintMatrix(matrix_in):
for x in range(0, matrix_in.shape[0]):
for y in range(0, matrix_in.shape[1]):
print("%d \t"%(matrix_in[x][y]), end='')
if(y%3>1):
print("\n")
# Function to populate a 2x2 matrix with random data
def FillMatrix(matrix_in):
for x in range(0, matrix_in.shape[0]):
for y in range(0, matrix_in.shape[1]):
matrix_in[x][y] = random.randrange(2, 10) + 2
# Create matrices using ndarray
matrix1 = np.ndarray((3,3))
matrix2 = np.ndarray((3,3))
# Fill the matrices i.e., the two dimensional arrays created using ndarray objects
FillMatrix(matrix1)
FillMatrix(matrix2)
# Add two matrices - two nd arrays
add_results = matrix1.__add__(matrix2)
# Print Matrix1
print("Matrix1:")
PrintMatrix(matrix1)
# Print Matrix2
print("Matrix2:")
PrintMatrix(matrix2)
# Print the results of adding two matrices
print("Result of adding Matrix1 and Matrix2:")
PrintMatrix(add_results)Output
Matrix1:
8 4 7
7 9 7
11 9 7
Matrix2:
5 7 5
7 11 10
9 11 5
Result of adding Matrix1 and Matrix2:
13 11 12
14 20 17
20 20 12