Python program to add two matrices
Last Updated :
06 Sep, 2024
Prerequisite : Arrays in Python, Loops, List Comprehension Program to compute the sum of two matrices and then print it in Python. We can perform matrix addition in various ways in Python. Here are a two of them. Examples:
Input :
X= [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
Output :
result= [[10,10,10],
[10,10,10],
[10,10,10]]
Using Nested Loops
Python
# Program to add two matrices using nested loop
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]
Time Complexity: O(N2), as we are traversing the matrix using nested loops.
Auxiliary Space: O(N*N), as we are using an extra space result matrix.
Explanation :- In this program we have used nested for loops to iterate through each row and each column. At each point we add the corresponding elements in the two matrices and store it in the result.
Using nested list comprehension
Here is another approach for addition of two matrix addition using nested list comprehension.
Python
# Program to add two matrices
# using list comprehension
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]
for r in result:
print(r)
Output:
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]
Explanation:- The output of this program is the same as above. We have used nested list comprehension to iterate through each element in the matrix.
Time Complexity: O(N2), as we are traversing the matrix using nested loops.
Auxiliary Space: O(N*N), as we are using an extra space result matrix.
Similar Reads
How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method:Pythona = [1, 2, 3] a.append(4) prin
2 min read
Adding and Subtracting Matrices in Python In this article, we will discuss how to add and subtract elements of the matrix in Python. Example: Suppose we have two matrices A and B. A = [[1,2],[3,4]] B = [[4,5],[6,7]] then we get A+B = [[5,7],[9,11]] A-B = [[-3,-3],[-3,-3]] Now let us try to implement this using Python 1. Adding elements of
4 min read
Merge Two Lists in Python Python provides several approaches to merge two lists. In this article, we will explore different methods to merge lists with their use cases. The simplest way to merge two lists is by using the + operator. Let's take an example to merge two lists using + operator.Pythona = [1, 2, 3] b = [4, 5, 6] #
4 min read
Python - Union of two or more Lists The union of two or more lists combines all elements ensuring no duplicates if specified. In this article we will explore various methods to get a union of two lists.Using set.union (Most Efficient for Uniqueness)The union() method ensures that the resulting list contains unique elements. Here we co
2 min read
Ways to Add Row/Columns in Numpy Array - Python Adding rows or columns to a NumPy array means appending new data along a specific axis. For example, if you have a 2D array like [[1, 2], [3, 4]] and you add a new row [5, 6], the array becomes [[1, 2], [3, 4], [5, 6]]. Similarly, adding a column [7, 8, 9] to a 3x2 array transforms it into a 3x3 arr
5 min read
How to append two NumPy Arrays? Prerequisites: Numpy Two arrays in python can be appended in multiple ways and all possible ones are discussed below. Method 1: Using append() method This method is used to Append values to the end of an array. Syntax : numpy.append(array, values, axis = None) Parameters : array: [array_like]Input a
4 min read