Python Program to Add Two Matrix Using Multi-dimensional Array or List (2 Ways)

In this program, we will learn how to add two matrix using a multi-dimensional array or list in Python programming language.

What is the Matrix?

A matrix is a rectangular array of numbers that are arranged in the form of rows and columns.
Example:-
A 3*3 matrix has 3 rows and 3 columns as shown below −
5 6 7
3 4 9
7 6 1

We will discuss two ways to code it.

  • Using Nested Loop
  • Using Nested List Comprehension

Python Programming Code to Add Two Matrix Using Multi-dimensional Array or List

Using Nested Loop

We will use the multi-dimensional list to store the matrices (a and b).

Firstly, User will be asked to enter the number of rows it required in the matrices and the same for the columns.

And then we will use the for loop to traverse through the multi-dimensional list to store the values.

Also nested for loop will be used to add the individual elements of the multi-dimensional list.

And then save the sum of the matrices in sum multi-dimensional list.

Code:-

rows=int(input("Enter the Number Rows: "));
cols=int(input("Enter the Number Coloums: "));

# Initializing Matrix a with 0s
a = [[0 for j in range(0,cols)] for i in range(0,rows)]  
# Initializing Matrix b with 0s 
b = [[0 for j in range(0,cols)] for i in range(0,rows)]    
# Initializing Matrix sum with 0s
sum = [[0 for j in range(0,cols)] for i in range(0,rows)] 

print("Enter the Elements of Matrix a:")
for i in range(0,rows):
    for j in range(0,cols):
        print("Enter an Element (",i+1,",",j+1,"):", sep="", end=" ")
        a[i][j]= int(input())

print("Enter the Elements of Matrix b:")	
for i in range(0,rows):
    for j in range(0,cols):
        print("Enter an Element (",i+1,",",j+1,"):", sep="", end=" ")
        b[i][j]=int(input())

# Adding two Matrices
for i in range(0,rows):
    for j in range(0,cols):
        sum[i][j]=a[i][j]+b[i][j]

# Displaying the Matrix sum
print("Sum of Matrices is")
for i in sum:
    print(i)

Output:-

Enter the Number Rows: 2
Enter the Number Coloums: 3
Enter the Elements of Matrix a:
Enter an Element (1,1): 8
Enter an Element (1,2): 5
Enter an Element (1,3): 4
Enter an Element (2,1): 6
Enter an Element (2,2): 9
Enter an Element (2,3): 2
Enter the Elements of Matrix b:
Enter an Element (1,1): 7
Enter an Element (1,2): 6
Enter an Element (1,3): 2
Enter an Element (2,1): 8
Enter an Element (2,2): 4
Enter an Element (2,3): 9
Sum of Matrices is
[15, 11, 6]
[14, 13, 11]

Using Nested List Comprehension

Code:-

rows=int(input("Enter the Number Rows: "));
cols=int(input("Enter the Number Coloums: "));

# Initializing Matrix a with 0s
a = [[0 for j in range(0,cols)] for i in range(0,rows)]  
# Initializing Matrix b with 0s 
b = [[0 for j in range(0,cols)] for i in range(0,rows)]    
# Initializing Matrix sum with 0s
sum = [[0 for j in range(0,cols)] for i in range(0,rows)] 

print("Enter the Elements of Matrix a:")
for i in range(0,rows):
    for j in range(0,cols):
        print("Enter an Element (",i+1,",",j+1,"):", sep="", end=" ")
        a[i][j]= int(input())

print("Enter the Elements of Matrix b:")	
for i in range(0,rows):
    for j in range(0,cols):
        print("Enter an Element (",i+1,",",j+1,"):", sep="", end=" ")
        b[i][j]=int(input())

# Adding two Matrices
sum = [[a[i][j] + b[i][j]  for j in range(len(a[0]))] for i in range(len(a))]

# Displaying the Matrix sum
print("Sum of Matrices is")
for i in sum:
    print(i)

Output:-

Enter the Number Rows: 3
Enter the Number Coloums: 3
Enter the Elements of Matrix a:
Enter an Element (1,1): 7
Enter an Element (1,2): 5
Enter an Element (1,3): 14
Enter an Element (2,1): 7
Enter an Element (2,2): 52
Enter an Element (2,3): 6
Enter an Element (3,1): 21
Enter an Element (3,2): 7
Enter an Element (3,3): 5
Enter the Elements of Matrix b:
Enter an Element (1,1): 12
Enter an Element (1,2): 75
Enter an Element (1,3): 3
Enter an Element (2,1): 51
Enter an Element (2,2): 17
Enter an Element (2,3): 24
Enter an Element (3,1): 9
Enter an Element (3,2): 56
Enter an Element (3,3): 24
Sum of Matrices is
[19, 80, 17]
[58, 69, 30]
[30, 63, 29]

You can learn about many other Python Programs Here.

Best Books for learning Python with Data Structure, Algorithms, Machine learning and Data Science.

The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...