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
Python Program to Multiply Two Matrices Given two matrices, we will have to create a program to multiply two matrices in Python. Example: Python Matrix Multiplication of Two-DimensionPythonmatrix_a = [[1, 2], [3, 4]] matrix_b = [[5, 6], [7, 8]] result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): result[i][j] = (matrix_a[i][0]
5 min read
Add Two Matrices - Python The task of adding two matrices in Python involves combining corresponding elements from two given matrices to produce a new matrix. Each element in the resulting matrix is obtained by adding the values at the same position in the input matrices. For example, if two 2x2 matrices are given as:Two 2x2
3 min read
Python Program for Kronecker Product of two matrices Given a {m} imes{n} matrix A and a {p} imes{q} matrix B, their Kronecker product C = A tensor B, also called their matrix direct product, is an {(mp)} imes{(nq)} matrix. A tensor B = |a11B a12B| |a21B a22B| = |a11b11 a11b12 a12b11 a12b12| |a11b21 a11b22 a12b21 a12b22| |a11b31 a11b32 a12b31 a12b32| |
3 min read
How to Add Two Numbers in Python The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12.Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perform
4 min read
How to Add Floats to a List in Python Adding floats to a list in Python is simple and can be done in several ways. The easiest way to add a float to a list is by using the append() method. This method adds a single value to the end of the list.Pythona = [1.2, 3.4, 5.6] #Add a float value (7.8) to the end of the list a.append(7.8) print(
2 min read
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
How to iterate two lists in parallel - Python Whether the lists are of equal or different lengths, we can use several techniques such as using the zip() function, enumerate() with indexing, or list comprehensions to efficiently iterate through multiple lists at once. Using zip() to Iterate Two Lists in ParallelA simple approach to iterate over
2 min read
Matrix Product - Python The task of calculating the matrix product in Python involves finding the product of all elements within a matrix or 2D list . This operation requires iterating through each element in the matrix and multiplying them together to obtain a single cumulative product. For example, given a matrix a = [[1
3 min read
Python Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re
4 min read
Python - Multiply Two Lists We are given two list we need to multiply given two list. For example, we are having two lists a = [1, 2, 3] b = [4, 5, 6] we need to multiply list so that the resultant output should be [4, 10, 18].Using a LoopWe can multiply two lists element-wise using a loop by iterating over both lists and mult
2 min read