Signup/Sign In

Python Program to add two Matrices

In this tutorial, we will learn to add two matrices in Python. A matrix is a two-dimensional data structure where numbers are arranged into rows and columns. Python does not have a built-in type for matrices but we can treat a list of a list as a matrix. The List is an ordered set of values enclosed in square brackets [ ].

Let A= [ [1, 2, 3] [4, 5, 6] [7, 8, 9] ] be a list of a list, then it can be treated as a 3x3 matrix with 3 rows and 3 columns

matrix

The first row can be selected as A[0] and the element in the first row, the first column can be selected as A[0][0]. To add two matrices we have to add the corresponding elements of both the matrices individually. For example,

Input:

A=[ [1,2], [3,4] ]

B=[ [1,3], [2,5] ]

Output: [ [2,5], [5,9] ]

To add two matrices in Python, we have to individually add each element in both the matrices which are at the same position in their respective matrix.

To access each element in the matrix, we can use:

  1. nested loops
  2. nested list comprehension

Approach 1: nested loops

For this approach, we will use nested loops which are simply a loop within a loop, to add elements of both the matrix and store it in a resultant matrix. The first loop will be for iterating through rows and the second loop will be for iterating through the columns.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function that will add two matrixes

Step 2- In the function declare a list that will store the result

Step 3- Iterate through the rows and columns

Step 4- Add the corresponding elements in the two matrices and store them in the result list

Step 5- Print the resultant list

Step 6- Declare and set values for two matrices

Step 7- Call the function, the result will be printed

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 3x3 matrix.

def addM(A,B):
    result=[ [0,0,0],[0,0,0],[0,0,0] ]
    #for rows
    for i in range(len(A)):
        #for columns
        for j in range(len(A[0])):
            result[i][j]=A[i][j] + B[i][j]

    for k in result:
        print(k)

    return 0
A=[ [1, 2, 3], [3, 4, 5], [6, 7, 8] ]

B=[ [5, 6, 7], [1, 2, 3], [5, 3, 8] ]

print("Result: ")
addM(A,B)


Result:
[6, 8, 10]
[4, 6, 8]
[11, 10, 16]

Approach 2: nested list comprehension

List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We will store the addition of the corresponding elements of both lists in a new list. We will use nested list comprehension to iterate through each element in the matrix. List comprehension allows us to write concise codes in Python.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function that will add two matrixes

Step 2- In the function, declare a list that will store the result

Step 3- Through list, comprehension add the corresponding elements and store them in the result list

Step 4- Print the resultant list

Step 5- Declare and set values for two matrices

Step 6- Call the function, the result will be printed

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 3x3 matrix. To get the addition of 2x2 matrix the result list will be initialised as result= [ [0, 0] [0, 0] ]

def addM(A,B):
    result=[ [0,0,0],[0,0,0],[0,0,0] ]
    #list comprehension
    result= [[A[i][j] + B[i][j]  for j in range(len(A[0]))] for i in range(len(A))]

    for k in result:
              print(k)

    return 0
A=[ [3,7, 9], [1, 2, 3], [4, 5, 10] ]

B=[ [3, 1, 1], [3, 4, 5], [7, 5, 6] ]

print("Result: ")
addM(A,B)


Result:
[6, 8, 10]
[4, 6, 8]
[11, 10, 16]

Conclusion

In this tutorial, we have learned what are matrices and how to add two matrices. We have used the nested loop and nested list comprehension for solving the problem.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.