Open In App

Add Two Matrices – Python

Last Updated : 20 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Two 2×2 Matrices

The sum of these matrices would be :

matrics_output

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:

  • a and b are two 3×3 NumPy arrays.
  • + operator applies element-wise addition using vectorization, making it fast and clean.
  • this approach avoids manual looping, making your code more concise and efficient.

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

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:

  • Manually loops through rows and columns.
  • Adds corresponding elements into res.
  • Best for educational or beginner-level code, but not the most efficient.

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



Next Article

Similar Reads