A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-program-add-two-matrices/ below:

Add Two Matrices - Python

Add Two Matrices - Python

Last Updated : 20 Apr, 2025

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 Matrices

The sum of these matrices would be :

Addition of the above matrices

Let's explore the various ways of adding two matrices in Python.

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. With NumPy, we can simply use the + operator for matrix addition.

Python
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:

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.

Python
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 3x3 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.

Python
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.

Python
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:

For in-depth understanding, also read: NumPy, List comprehension, zip(), nested loop.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4