Matrix Subtraction with user inputs using NumPy with Python
By Rupam Biswas
Here we are subtracting two matrices, which will be given by the user using NumPy in python.
Here, we are going to subtract two matrices in python using NumPy. Those Matrices will be given by the user.
Here is a mathematical example of subtracting two matrices,
Step 1: We have to install NumPy in python.
For that, if you are using pip, you can install NumPy with:
pip install numpy
Step 2: Here, we are importing NumPy as np.
Step 3: Then, we are taking row and column inputs from the user.
Step 4: Then, we have to take the matrix values from the user for two different matrices.
Step 5: Print those matrices.
Step 6: Subtracting and printing the final matrix.
Here is the code:
#Importing NumPy and giving the name as np
import numpy as np
row = int(input("Enter the number of rows ="))
column = int(input("Enter the number of columns ="))
# Taking the inputs from th user for two matrixes
print("Enter the elements separated by space for first matrix =")
ele1 = list(map(int, input().split()))
print("Enter the elements separated by space for second matrix =")
ele2 = list(map(int, input().split()))
# Defining those as Matrix1 and Matrix2
matrix1 = np.array(ele1).reshape(row, column)
matrix2 = np.array(ele2).reshape(row, column)
# Printing the matrixes
print("Printing elements of first matrix")
print("Printing elements of second matrix")
# subtraction between two matrixes
print("Subtraction of two matrix")