Matrix Subtraction in Python | Here, we will discuss matrix subtraction in python. For subtracting any two matrices, both the matrices should be of the same dimension; we carry out the subtraction by subtracting corresponding elements together. Similar to the subtract operation, we can also implement other mathematical operations. Also See:- Matrix Addition Program in Python
We will see these below topics:–
- Matrix Subtraction In Python Using NumPy
- Matrix Subtraction In Python User Input
- Matrix Subtraction In Python Using Function
- Matrix Subtraction In Python Using Nested Loop
Python Program to Subtract Two Matrix Using NumPy
Numpy is a library in python which has several functions that make it easy for the programmer to concentrate on the logic part. In this section, we use NumPy for the subtraction of two matrices in python.
In the code, we have imported NumPy as np then declared matrix 1 and matrix 2 as m1 and m2 respectively by using numpy.subtract(). We subtract m1 and m2 and store the result in r.
Program description:- Write a python program to subtract two matrices
import numpy as np m1 = np.array([[7, 2, 8], [2, 3, 6]]) m2 = np.array([[8, 0, 2], [4, 1, 6]]) r = np.subtract(m2, m1) print(r)
[[ 1 -2 -6]
[ 2 -2 0]]
Matrix Subtraction in Python User Input
Here we subtract matrices by taking user input, we use a list and map. We use lists to store multiple items of a single variable, it is one of the data types in python. A map is an inbuilt function that allows us to process and transform all the items in an iterable without using a loop.
# Python program to subtract two matrix # Take first matrix inputs m1 = [list(map(int, input("Enter row: ").split(" "))) for i in range(int(input("Enter Number of rows: ")))] # Take second matrix inputs m2 = [list(map(int, input("Enter row: ").split(" "))) for i in range(int(input("Enter Number of rows: ")))] # subtract these matrix print("Subtraction of Matrix:") r = [[m1[i][j] - m2[i][j] for j in range(len(m1))] for i in range(len(m1))] print(r)
Enter Number of rows: 3
Enter row: 45 65 23
Enter row: 78 98 56
Enter row: 24 36 57
Enter Number of rows: 3
Enter row: 12 78 34
Enter row: 78 23 10
Enter row: 25 46 32
Subtraction of Matrix:
[[33, -13, -11], [0, 75, 46], [-1, -10, 25]]
In the above code, we first take the matrix from the user and store it in m1, then similarly we take m2 and we read all the row elements as well. Then we subtract the matrix and put it in the result. While subtracting, we use a for loop to check whether the dimension is the same or not.
In the output shown first it takes the number of rows for matrix 1 and reads the matrix elements similarly it is done for matrix 2 then it subtracts and gives the result.
Matrix Subtraction in Python Using Function
In python we can define our own function, so by using this we can subtract two matrices. Here in the below code, we have defined a function called subtract() which takes two parameters m1 and m2.
def subtract(m1, m2): new =  for i in range(0, len(m1)): new.append(m1[i] - m2[i]) return new # main m1 = [[9, 8], [9, 0]] m2 = [[8, 8], [6, 5]] c =  for i in range(0, len(m1)): c.append(subtract(m1[i], m2[i])) print("Subtract Matrix:") print(c)
[[1, 0], [3, -5]]
In the subtract() function, we initiate new to an empty array, then we append new to the subtraction of matrix 1 and matrix2. Then in the main function, we take two matrices and then call subtract() to subtract the matrices and store them in c.
Matrix Subtraction Program in Python using a Nested Loop
A nested loop is a loop inside another loop. In this example, we use nested for-loops to subtract matrices.
Program description:- Write a python program to subtract two matrices taking input from a user
# Python program to subtract two matrices r = int(input("Enter the rows: ")) c = int(input("Enter the columns: ")) print("Enter Matrix 1:") m1 = [[int(input()) for i in range(c)] for i in range(r)] print("Matrix 1 is: ") for n in m1: print(n) print("Enter Matrix 2:") m2 = [[int(input()) for i in range(c)] for i in range(r)] for n in m2: print(n) r = [[0 for i in range(c)] for i in range(r)] for i in range(len(r)): for j in range(c): r[i][j] = [m1[i][j] - m2[i][j]] print("Subtract Matrix:") for i in r: print(i)
Enter the rows: 2
Enter the columns: 3
Enter Matrix 1:
Matrix 1 is:
[45, 56, 23]
[98, 75, 65]
Enter Matrix 2:
[20, 35, 12]
[65, 45, 36]
[, , ]
[, , ]
In the above code, we have used a nested for loop to subtract m1 and m2. The outer loop reads the row elements the inner loop reads the column elements and stores the result in r.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!