Matrix addition in Python
In this post, you will learn different ways to perform matrix addition using the Python programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop, it uses matrices to process linear transformations to render images. In robotics and automation, matrices are the fundamental segments for the robot developments. The contributions for controlling robots are acquired depending on the calculations from matrices.
The different operations on the matrix are also very important for us. There are different ways to perform matrix addition in Python.
Matrix Addition using Nested Loop
A nested loop is a loop inside a loop. The following program uses a nested for loop to iterate through each row and each column. In each iteration, we add the corresponding elements in the two matrices and store it in the result.
# Program to add two matrices using nested loop
x = [[5,2,1],
[6,15,20],
[5,18,32]]
y = [[0,1,8],
[8,5,2],
[2,4,6]]
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 of the above code -
[5, 3, 9]
[14, 20, 22]
[7, 22, 38]
Matrix Addition using Nested List Comprehension
Nested List Comprehensions are nothing but a list comprehension within another list comprehension. It is quite similar to a nested loop. The following program uses a nested list comprehension to iterate through each element in the matrix.
# Program to add two matrices using nested list comprehension
x = [[5,2,1],
[6,15,20],
[5,18,32]]
y = [[0,1,8],
[8,5,2],
[2,4,6]]
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 of the above code -
[5, 3, 9]
[14, 20, 22]
[7, 22, 38]
Matrix Addition using zip() and sum
In the given program, we perform the matrix addition using zip() and sum. The zip() function accepts an iterator of each element of the matrix, mapping them, adding them using the sum() method and storing them in the map form.
# Program to add two matrices
# using zip()
X = [[11,6,8],
[9,3,2],
[9,1,6]]
Y = [[2,9,3],
[4,8,1],
[4,9,2]]
result = [map(sum, zip(*t)) for t in zip(X, Y)]
print(result)
Output of the above code:
[[13, 15, 11], [13, 11, 3], [13, 10, 8]]
Related Articles
Python multiline string
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python compare two lists
Insert data in MySQL database from an HTML form using Django
Alphabet pattern programs in Python