Add Two Matrices Using Multi-Dimensional Array in Python



A matrix is a two-dimensional array of many numbers arranged in rows and columns. The addition of two matrices is a process of adding corresponding elements of two matrices and placing the sum in corresponding position of the resultant matrix. And this can be possible, only if both the matrices have an equal number of rows and columns.

In Python multidimensional arrays are created by using lists or NumPy arrays. The list data structure can accept lists as elements so that we can easily create matrice. Also, the Numpy module provides numerous methods to work with multidimensional arrays.

Input Output Scenarios

The addition of two matrices

[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]

In this article, we will see how to add two matrix using multidimensional arrays in python.

Using For Loop

Here we will use a nested for loop to iterate through each row and each column of the given input matrices. And at each iteration, we will add the corresponding elements of the two input matrices and store them in the result matrix.

Example

Open Compiler
# Defining the matrix 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 two input matrices print('The first matrix is defined as:') display(matrix_a) print('The second matrix is defined as:') display(matrix_b) # Initializing Matrix with all 0s result = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] # Add two matrices for i in range(len(matrix_a)): # iterate through rows for j in range(len(matrix_a[0])): # iterate through columns result[i][j] = matrix_a[i][j] + matrix_b[i][j] print('The addition of two matrices is:') display(result)

Output

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]

The sum of the corresponding elements of two input matrices are stored in the result matrix which we created initially with all zeros.

Using List Comprehension

List comprehension provides the shortest syntax to build a list without initializing an empty list before the for loops to append values one by one.

Example

This example works similarly to the previous example but the difference is here we have used the list comprehension instead of creating the result matrix with all zeros.

Open Compiler
# Defining the matrix 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 two input matrices print('The first matrix is defined as:') display(matrix_a) print('The second matrix is defined as:') display(matrix_b) # Add two matrices 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)

Output

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

The NumPy module in python has many built-in functions to work with multidimensional arrays. By using these arrays we can easily add the two matrices.

Example

In this example, we will create two multidimensional arrays using the numpy.array() method. And then applied the addition operator between the two arrays.

Open Compiler
import numpy as np # Defining the matrix using numpy array 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 two input matrices print('The first matrix is defined as:') print(matrix_a) print('The second matrix is defined as:') print(matrix_b) # Add two matrices result = matrix_a + matrix_b print('The addition of two matrices is:') print(result)

Output

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

We simply applied the addition operator (+) between numpy arrays matrix_a, matrix_b to add the multidimensional arrays.

Updated on: 2023-05-15T16:32:53+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements