-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_6.py
More file actions
40 lines (34 loc) · 921 Bytes
/
Copy pathproject_6.py
File metadata and controls
40 lines (34 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Program to Add Two Matrices
# Function to add two matrices
def add_matrices(mat1, mat2):
# Check if the matrices have the same dimensions
if len(mat1) != len(mat2) or len(mat1[0]) != len(mat2[0]):
return "Matrices must have the same dimensions for addition"
# Initialize an empty result matrix with the same dimensions
result = []
for i in range(len(mat1)):
row = []
for j in range(len(mat1[0])):
row.append(mat1[i][j] + mat2[i][j])
result.append(row)
return result
# Input matrices
matrix1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
# Call the add_matrices function
result_matrix = add_matrices(matrix1, matrix2)
# Display the result
if isinstance(result_matrix, str):
print(result_matrix)
else:
print("Sum of matrices:")
for row in result_matrix:
print(row)