-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrices.py
43 lines (34 loc) · 882 Bytes
/
Matrices.py
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
41
42
43
def add_matrices(matrix1, matrix2):
# Check if matrices have the same dimensions
if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
raise ValueError("Matrices must have the same dimensions for addition.")
result_matrix = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] + matrix2[i][j])
result_matrix.append(row)
return result_matrix
# Example matrices
matrix_a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix_b = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
# Add matrices
result_matrix = add_matrices(matrix_a, matrix_b)
# Display the result
print("Matrix A:")
for row in matrix_a:
print(row)
print("\nMatrix B:")
for row in matrix_b:
print(row)
print("\nResultant Matrix:")
for row in result_matrix:
print(row)