Here in this article, we are going to provide source code for a python program that adds two matrices using the nested loop .
A matrix is simply defined as some arrangement of numbers that are formed by using columns and rows. In Python, we use a list of lists to represent a matrix. If each element of the list is itself a list then it would be a matrix. For instance, [[1,2,3],[3,5,3], [7,9,10]] is a 3x3 matrix i.e. it has 3 rows and 3 columns.
Matrix Addition
The number of rows and columns in a matrix determines its dimensions. For example, [2, 3, 4, 5, 22, 24] is a one-dimensional array while [[2,3], [4,5], [22,24]] is a 2-dimensional array.
Following is an example of a 3x3 matrix: [ [1,2,3], [3,5,3], [7,9,10] ]
To add two matrices, their dimensions should be similar. For example, you can add a 2x3 matrix only with another matrix with 2 rows and 3 columns.
Prerequisites to Create the Python Program to Add Two Matrices
In order to comprehend and write the program for matrix addition, you need to have a sound understanding of the following concepts in Python:
- Input/Output
- Lists
- Looping
- List comprehension
Program Steps
The program that we are going to discuss next will follow the below-mentioned sequence:
- Ask the user to enter the number of rows and columns for the matrices to be added.
- Now, use the list comprehension and input() function to ask the user to enter the elements for both the matrices.
- Create a result matrix with the same dimension of both the matrices and keep its all element as 0.
- Using the Nested for loop, add the element of the first matrix with the other and store the result in the result matrix.
- Display the output.
Python Program to Add Two Matrices
Python Code:
#matrix addition in python
rows = int(input("Enter the Number of rows : " ))
column = int(input("Enter the Number of Columns: "))
print("Enter the elements of First Matrix:")
matrix_a= [[int(input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
print(n)
print("Enter the elements of Second Matrix:")
matrix_b= [[int(input()) for i in range(column)] for i in range(rows)]
for n in matrix_b:
print(n)
result=[[0 for i in range(column)] for i in range(rows)]
for i in range(rows):
for j in range(column):
result[i][j] = matrix_a[i][j]+matrix_b[i][j]
print("The Sum of Above two Matrices is : ")
for r in result:
print(r)
Sample Output 1:
Enter the Number of rows : 3
Enter the Number of Columns: 3
Enter the elements of First Matrix:
2
4
6
7
2
9
23
12
35
First Matrix is:
[2, 4, 6]
[7, 2, 9]
[23, 12, 35]
Enter the elements of Second Matrix:
6
20
10
14
15
28
29
40
11
[6, 20, 10]
[14, 15, 28]
[29, 40, 11]
The Sum of Above two Matrices is :
[8, 24, 16]
[21, 17, 37]
[52, 52, 46]
Sample Output 2:
Enter the Number of rows : 2
Enter the Number of Columns: 4
Enter the elements of First Matrix:
1
2
3
4
5
6
7
8
First Matrix is:
[1, 2, 3, 4]
[5, 6, 7, 8]
Enter the elements of Second Matrix:
10
12
24
12
34
54
2
34
[10, 12, 24, 12]
[34, 54, 2, 34]
The Sum of Above two Matrices is :
[11, 14, 27, 16]
[39, 60, 9, 42]
Matrix Addition in Python using Numpy
Numpy is a popular Python library that is famous for its arrays and matrix operations. In the above section, we learned how to add two matrices in Python using the nested for loop. Now let's discuss how can we use a numpy library and perform the same matrix addition in Python.
Note:
To use the numpy library, we first need to install it. To install the numpy library for our Python environment, we can use the
pip install numpy
command.
Example: Addition of two matrices in Python using numpy add method
Similar to the above program, we first accept the user input matrix as a list, and convert it into the
Python numpy matrix
using numpy
array()
method and then perform the addition operation.
import numpy as np
#matrix addition in python using numpy
rows = int(input("Enter the Number of rows : " ))
column = int(input("Enter the Number of Columns: "))
#accept the first matix
print("Enter the elements of First Matrix:")
matrix_a= [[int(input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
print(n)
#convert the first matrix into numpy array
matrix_a = np.array(matrix_a)
#accept the second matix
print("Enter the elements of Second Matrix:")
matrix_b= [[int(input()) for i in range(column)] for i in range(rows)]
for n in matrix_b:
print(n)
#convert the second matrix into numpy array
matrix_b = np.array(matrix_b)
#add two matrices in python using numpy
result = np.add(matrix_a, matrix_b)
print("The Sum of Above two Matrices is : ")
for r in result:
print(r)
Output
Enter the Number of rows : 3
Enter the Number of Columns: 2
Enter the elements of First Matrix:
2
3
1
3
4
1
First Matrix is:
[2, 3]
[1, 3]
[4, 1]
Enter the elements of Second Matrix:
3
4
2
4
5
2
[3, 4]
[2, 4]
[5, 2]
The Sum of Above two Matrices is :
[5 7]
[3 7]
[9 3]
Conclusion
That was all about adding two matrices in Python. You are free to tweak the code to learn more about the concept of matrix addition. You can also get creative and implement the program in some other way that you find suitable and feasible.
Want to share your awesome code with us? Use the dedicated comments section below to do the same.
To learn, practice, and solve more challenging programs in Python, purchase the course here.
People are also reading:
- Find whether a number is a palindrome or not
- WAP to display a message on the screen
- Perfect Number in C
- Print the sum of first n natural numbers
- Print the 1 to 10 Multiples of a Number
- Linear Search in Python and C++
- Calculate 3X3 Matrix multiplication
- Extract a Portion of String
- How many Centimeters in a Foot through Python?
- Convert a given number of days into years, weeks and days
Leave a Comment on this Post