Skip to content

Commit 4bd9fb9

Browse files
Create multiply_matrices.py
i have added new program of multiply_matrices
1 parent 025f8e5 commit 4bd9fb9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

multiply_matrices.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def multiply_matrices(matrix_a, matrix_b):
2+
rows_a = len(matrix_a)
3+
cols_a = len(matrix_a[0])
4+
cols_b = len(matrix_b[0])
5+
6+
result = [[0 for _ in range(cols_b)] for _ in range(rows_a)]
7+
8+
for i in range(rows_a):
9+
for j in range(cols_b):
10+
for k in range(cols_a):
11+
result[i][j] += matrix_a[i][k] * matrix_b[k][j]
12+
13+
return result
14+
15+
16+
matrix_a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
17+
matrix_b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
18+
19+
result_matrix = multiply_matrices(matrix_a, matrix_b)
20+
21+
# Displaying the result
22+
for row in result_matrix:
23+
print(" ".join(map(str, row)))

0 commit comments

Comments
 (0)