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 Addition
Photo Credit to CodeToFun
π Introduction
Matrix addition is a fundamental operation in linear algebra and computer science. It involves adding corresponding elements of two matrices to create a new matrix.
This operation is essential in various fields, including computer graphics, scientific computing, and data analysis.
In this tutorial, we'll explore a Python program that performs matrix addition.
Understanding matrix operations and implementing them in C can provide a solid foundation for more complex computations.
π Example
Let's delve into the Python code that achieves matrix addition.
# Function to perform matrix addition
def add_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(" ".join(str(element) for element in row))
# Sample matrices for testing
matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
# Perform matrix addition
result_matrix = add_matrices(matrix1, matrix2)
# Displaying matrices and result
print("Matrix 1:")
display_matrix(matrix1)
print("\nMatrix 2:")
display_matrix(matrix2)
print("\nResultant Matrix:")
display_matrix(result_matrix)
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices in the code.
Matrix 1: 1 2 3 4 5 6 7 8 9 Matrix 2: 9 8 7 6 5 4 3 2 1 Resultant Matrix: 10 10 10 10 10 10 10 10 10
Run the script to see the result of the matrix addition.
π§ How the Program Works
- The program defines a function add_matrices to add two matrices and a function display_matrix to display a matrix.
- Sample matrices matrix1 and matrix2 are provided for testing.
- The add_matrices function is called to perform the addition, and the result is stored in result_matrix.
- The matrices are displayed using the display_matrix function.
π§ Understanding the Concept of Matrix Addition
Matrix addition involves adding corresponding elements of two matrices to create a new matrix.
Given two matrices A and B, the sum C is calculated as follows:
Cij = Aij + Bij.
where Cij is the element at the i-th row and j-th column of matrix C, and Aij and Bij are the corresponding elements of matrices A and B.
Matrix addition is only defined for matrices of the same size; that is, they must have the same number of rows and columns.
π’ Optimizing the Program
While the provided program is effective, there are optimizations that can be applied for larger matrices. Consider exploring techniques such as parallelization for improved performance.
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 Addition), please comment here. I will help you immediately.