## Python Program to Add Two Matrices

This discussion is about "Python Program to Add Two Matrices" in the "General Gizmos & Gadgets Discussion" forums.
Hi this is Katy Steven I am in 3rd year of engineering, and as my interest is towards learning Python, I have start learning and ...

1. ## Python Program to Add Two Matrices

Hi this is Katy Steven
I am in 3rd year of engineering, and as my interest is towards learning Python, I have start learning and coding of Python. Now I want to learn how to add two matrix in python. Can anyone suggest me detailed information of this. In short I have require a Python program to add two matrix in Python.

Hope I will get positive reply from you guys.

Thanks

2. To add two matrices in Python, you can use the following steps:

1. Define two matrices as nested lists.
2. Iterate through each row and column of the matrices, adding the corresponding elements and storing the result in a new matrix.
3. Print the new matrix.

Here is a short Python program to add two matrices:

sum_matrix = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] + matrix2[i][j])
sum_matrix.append(row)
return sum_matrix

# Example usage:

matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]

sum_matrix = add_matrices(matrix1, matrix2)

print(sum_matrix)

Output:
[[6, 8], [10, 12]]
You can also use the NumPy library to add two matrices in Python development. To do this, you would first need to convert the two matrices to NumPy arrays, and then use the numpy.add() function to add the two matrices.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•