Open In App

Python Program to Add Two Matrices

Last Updated : 22 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 2×2 matrices are given as:

matrics

The sum of these matrics would be :

matrics_output

Using numpy

NumPy is the most efficient solution for adding two matrices in Python. It is designed for high-performance numerical operations and matrix addition is natively supported using vectorized operations.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

res = a + b

print(res)

Output
[[10 10 10]
 [10 10 10]
 [10 10 10]]

Explanation: Two 3×3 matrices, a and b are created using np.array(). Matrix addition is performed with the + operator, which applies element-wise addition through vectorization, eliminating explicit loops.

Using list comprehension

List comprehension is a faster alternative to traditional loops when adding two matrices. It allows performing element-wise operations in a single line, improving readability and execution speed, especially for small to medium-sized matrices.

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

res = [[a[i][j] + b[i][j] for j in range(len(a[0]))] for i in range(len(a))]

for r in res:
    print(r)

Output
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Explanation: list comprehension iterates over rows and columns, adding corresponding elements from two 3×3 matrices a and b, storing the result in res.

Using zip()

Combining zip() with list comprehension is a clean and readable approach to adding two matrices. It aligns corresponding elements from both matrices and adds them, reducing indexing complexity while enhancing code readability and performance for small datasets.

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

res = [[x + y for x, y in zip(row_a, row_b)] for row_a, row_b in zip(a, b)]

for r in res:
    print(r)

Output
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Explanation: It pairs corresponding rows of matrices a and b using zip() and within each row, pairs elements with zip() again, adding them using list comprehension.

Using nested loops

The traditional nested loop method is the most basic approach to adding two matrices. It manually iterates through each element of the matrices, making it clear for beginners but less efficient and slower for larger datasets compared to modern alternatives.

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

res = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(len(a)):
    for j in range(len(a[0])):
        res[i][j] = a[i][j] + b[i][j]

for r in res:
    print(r)

Output
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

Explanation: It iterates over each row and column using two for loops, adding corresponding elements from matrices a and b, and stores the result in res.



Next Article

Similar Reads

three90RightbarBannerImg