Adding and Subtracting Matrices in Python

Configurare noua (How To)

Situatie

Suppose we have two matrices A and B.
A = [[1,2],[3,4]]
B = [[4,5],[6,7]]

then we get
A+B = [[5,7],[9,11]]
A-B = [[-3,-3],[-3,-3]]

Backup

# importing numpy as np
import numpy as np
 
 
# creating first matrix
A = np.array([[1, 2], [3, 4]])
 
# creating second matrix
B = np.array([[4, 5], [6, 7]])
 
print("Printing elements of first matrix")
print(A)
print("Printing elements of second matrix")
print(B)
 
# adding two matrix
print("Addition of two matrix")
print(np.add(A, B))

Solutie

Tip solutie

Permanent

Voteaza

(2 din 3 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?