 # Python Program to Add Two Matrices

There are numerous methods for matrix addition python. This can be done using the for loop, using nested list comprehension, and also by using the zip().

For example:

Input :

                    
X= [[8,6,3],

[4 ,5, 6],

[7 ,8, 3]]

Y = [[2,5,5],

[6,5,5],

[3,2,2]]



Output :

                    
result= [[10,11,8],

[10,10,11],

[10,10,5]] Source Code

Source code: Matrix Addition using Nested Loop

In the matrix addition python program using the nested loops, the program is used to iterate through every row and every column. The compiler adds the corresponding elements in the two matrices at each position and stores the result. A matrix can be implemented as a hierarchical list in Python (list inside a list). Each element in the matrix can be thought of as a row.

Source Code

                    
A = [[12,7,3],

[4 ,5,6],

[7 ,8,9]]

B = [[5,8,1],

[6,7,3],

[4,5,9]]

result = [[0,0,0],

[0,0,0],

[0,0,0]]

# iterate through rows

for x in range(len(A)):

# iterate through columns

for y in range(len(A)):

result[x][y] = A[x][y] + B[x][y]

for q in result:

print(q)

Output

[17, 15, 4]

[10, 12, 9]

[11, 13, 18]



Source Code: Matrix Addition using Nested List Comprehension

                    
matrix1 = [[1,1,1], [2,2,2], [3,3,3]]

matrix2 = [[4,6,4], [5,6,5], [6,5,6]]

# The matrices are added using the for loop.

# Assume that the matrices are of the same size.

matrixsum = []

for row in range(len(matrix1)):

matrixsum.append([])

for column in range(len(matrix)):

matrixsum[row].append(matrix1[row][column] + matrix2[row][column])

#Only use list comprehension for inner lists when rewriting.

matrixsum = []

for row in range(len(matrix1)):

matrixsum.append([matrix1[row][column] + matrix2[row][column] for column in range(len(matrix))])

# When rewriting use the nested list comprehension.

matrixsum = [[matrix1[row][column] + matrix2[row][column] for column in range(len(matrix))]

for row in range(len(matrix1))]

print(matrixsum)

Output:

[[5, 7, 5], [7, 8, 7], [9, 8, 9]]



## How do you add to a matrix in python?

Suppose we have two matrices E and D.

Example 1:

E = [[1,2],[3,4]]

DY = [[4,5],[6,7]]

then we get

E+D = [[5,7],[9,11]]

Example 2:

E = [[1,2],[3,4],[8,3]]

D = [[4,5],[6,7],[4,7]]

then we get

E+D = [[5,7],[9,11],[12,10]]

Look at the example given below to understand how to add the elements in Python:

Source Code:

                    
import numpy as np

# The first matrix will be

P = np.array([[1, 2], [3, 4]])

#The second matrix will be

Q = np.array([[4, 5], [6, 7]])

print("Elements of the first matrix")

print(P)

print("Elements of the second matrix")

print(Q)

print("The sum of the two matrices is")

Output:

Elements of the first matrix

[[1 2]

[3 4]]

Elements of the second matrix

[[4 5]

[6 7]]

The sum of the two matrices is

[[ 5 7]

[ 9 11]]



## How do you find the sum of a matrix in python?

The sum of the matrices is obtained using the for loop, using nested list comprehension, and by using the zip().

Source Code for finding the sum of the matrices using the zip():

The zip() function takes an iterator for each matrix element, maps them, and then adds them using the sum() function. It returns the result and saves it in the mapping form.

                    
X = [[1,2,3],

[4 ,0,6],

[7 ,8,9]]

Y = [[9,4, 7],

[6,5,2],

[3,2,6]]

result1= [map(sum, zip(*t)) for t in zip(X, Y)]

print(result)



## Can you add a 2×2 and a 2×3 matrix?

No. It is not possible to add a 2×2 and a 2×3 matrix. For the addition of matrices, it is important that the matrices have the same dimensions. Adding a 2×2 and 2×3 matrix is not feasible, but you can add a 2×3 matrice with a 2×3 or a 3×3 with a 3×3

## How do you add two matrices in Python using Numpy?

Using the matrix.sum() we can add the sum of two matrices.

Syntax :

matrix.sum()

Return:

Return the sum of a matrix’s values.

Example 1:

                    
import numpy as np

#Numpy can be used to create matrices.

abc = np.matrix('[4, 1; 12, 3]')

# applying matrix.sum() method

g = abc.sum()

print(g)

Output:

20




Example 2:

                    
import numpy as np

# make matrix with numpy

abc = np.matrix('[4, 1; 12, 3]')

# applying matrix.sum() method to obtain the sum of the matrices

gg = abc.sum()

print(gg)
Output:

20


Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.  Ashhar Firdausi
IIT Roorkee
Biology  Dr. Nazma Shaik
VTU
Chemistry  Gaurav Tiwari
APJAKTU
Physics
Get Started 