Python Program to Add Two Matrix Using Multi-dimensional Array

A matrix is a two-dimensional array of numbers arranged in rows and columns. The addition of two matrices involves adding corresponding elements and placing the sum in the corresponding position of the resultant matrix. This operation is only possible when both matrices have equal dimensions.

In Python, multidimensional arrays are created using lists or NumPy arrays. The list data structure can accept lists as elements, making it easy to create matrices. The NumPy module provides numerous methods to work efficiently with multidimensional arrays.

Matrix Addition Formula

For two matrices A and B, the addition follows this pattern ?

[a11, a12, a13]   [b11, b12, b13]   [a11+b11, a12+b12, a13+b13]
[a21, a22, a23] + [b21, b22, b23] = [a21+b21, a22+b22, a23+b23]
[a31, a32, a33]   [b31, b32, b33]   [a31+b31, a32+b32, a33+b33]

Using For Loop

We use nested for loops to iterate through each row and column of the input matrices. At each iteration, we add corresponding elements and store the result ?

# Defining matrices using multidimensional arrays
matrix_a = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]
 
matrix_b = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]

# Function for displaying matrix
def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display input matrices
print('The first matrix is defined as:') 
display(matrix_a)
print('The second matrix is defined as:')
display(matrix_b)

# Initialize result matrix with zeros
result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# Add two matrices using nested loops
for i in range(len(matrix_a)):
    for j in range(len(matrix_a[0])):
        result[i][j] = matrix_a[i][j] + matrix_b[i][j]

print('The addition of two matrices is:')
display(result)
The first matrix is defined as:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

The second matrix is defined as:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

The addition of two matrices is:
[2, 4, 6]
[8, 10, 12]
[14, 16, 18]

Using List Comprehension

List comprehension provides a concise syntax to create the result matrix without initializing with zeros ?

# Defining matrices using multidimensional arrays
matrix_a = [[1, 2, 5],
            [1, 0, 6],
            [9, 8, 0]]
 
matrix_b = [[0, 3, 5],
            [4, 6, 9],
            [1, 8, 0]]

# Function for displaying matrix
def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display input matrices
print('The first matrix is defined as:') 
display(matrix_a)
print('The second matrix is defined as:')
display(matrix_b)

# Add matrices using list comprehension
result = [[matrix_a[i][j] + matrix_b[i][j] for j in range(len(matrix_a[0]))] for i in range(len(matrix_a))]

print('The addition of two matrices is:')
display(result)
The first matrix is defined as:
[1, 2, 5]
[1, 0, 6]
[9, 8, 0]

The second matrix is defined as:
[0, 3, 5]
[4, 6, 9]
[1, 8, 0]

The addition of two matrices is:
[1, 5, 10]
[5, 6, 15]
[10, 16, 0]

Using NumPy Array

NumPy provides built-in functions for multidimensional array operations. Matrix addition becomes a simple operator application ?

import numpy as np

# Create matrices using NumPy arrays
matrix_a = np.array([[1, 2, 5], [1, 0, 6], [9, 8, 0]])
matrix_b = np.array([[0, 3, 5], [4, 6, 9], [1, 8, 0]])

# Display input matrices
print('The first matrix is defined as:') 
print(matrix_a)

print('The second matrix is defined as:')
print(matrix_b)

# Add matrices using + operator
result = matrix_a + matrix_b

print('The addition of two matrices is:')
print(result)
The first matrix is defined as:
[[1 2 5]
 [1 0 6]
 [9 8 0]]
The second matrix is defined as:
[[0 3 5]
 [4 6 9]
 [1 8 0]]
The addition of two matrices is:
[[ 1  5 10]
 [ 5  6 15]
 [10 16  0]]

Comparison

Method Readability Performance Best For
For Loop High Slow Learning concepts
List Comprehension Medium Medium Pure Python solutions
NumPy High Fast Scientific computing

Conclusion

Matrix addition can be implemented using nested loops, list comprehension, or NumPy arrays. NumPy provides the most efficient solution for large matrices, while loops offer better understanding of the underlying process.

Updated on: 2026-03-27T06:39:33+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements