Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Perform Matrix Subtraction
Photo Credit to CodeToFun
π Introduction
Matrix subtraction is a fundamental operation in linear algebra and computer science.
Matrix subtraction involves subtracting each element of one matrix from the corresponding element of another matrix of the same order.
In this tutorial, we'll explore a python program that performs matrix subtraction.
In the provided python program, we'll perform matrix subtraction for 3x3 matrices as an example. You can easily modify the program to work with matrices of different sizes.
π Example
Let's dive into the python code that performs matrix subtraction.
# Function to perform matrix subtraction
def subtract_matrices(mat1, mat2):
result_matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(3):
for j in range(3):
result_matrix[i][j] = mat1[i][j] - mat2[i][j]
return result_matrix
# Function to display a matrix
def display_matrix(matrix):
for row in matrix:
print('\t'.join(map(str, row)))
print()
# Sample matrices for subtraction
matrix1 = [[5, 8, 2], [7, 4, 9], [3, 6, 1]]
matrix2 = [[3, 1, 7], [6, 9, 2], [8, 5, 4]]
# Perform matrix subtraction
result_matrix = subtract_matrices(matrix1, matrix2)
# Display matrices and result
print("Matrix 1:")
display_matrix(matrix1)
print("Matrix 2:")
display_matrix(matrix2)
print("Resultant Matrix (Matrix1 - Matrix2):")
display_matrix(result_matrix)
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.
Matrix 1: 5 8 2 7 4 9 3 6 1 Matrix 2: 3 1 7 6 9 2 8 5 4 Resultant Matrix (Matrix1 - Matrix2): 2 7 -5 1 -5 7 -5 1 -3
Run the program to see the result of the matrix subtraction.
π§ How the Program Works
- The program defines a function subtract_matrices that takes two matrices as input and returns their subtraction in a new matrix.
- Another function display_matrix is defined to print a matrix.
- Sample 3x3 matrices matrix1 and matrix2 are defined, and matrix subtraction is performed using the subtract_matrices function.
- The result is displayed using the display_matrix function.
π§ Understanding the Concept of Matrix Subtraction
Matrix subtraction is performed element-wise, subtracting the corresponding elements of one matrix from the elements of another matrix. The result is a new matrix of the same order.
In the provided example, the element in the first row and first column of the result matrix is obtained by subtracting the element in the first row and first column of Matrix 2 from the element in the first row and first column of Matrix 1, and so on.
π Conclusion
Matrix subtraction is a fundamental operation in linear algebra and computer science. This C program provides a practical implementation of matrix subtraction and serves as a foundation for more advanced matrix operations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Perform Matrix Subtraction), please comment here. I will help you immediately.