求两个矩阵的乘法PYTHON实现
时间: 2023-09-03 13:15:14 浏览: 149
用python实现矩阵乘法
以下是两个矩阵的乘法Python实现代码:
```python
# 定义矩阵A和B
A = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
B = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
# 计算矩阵A和B的乘积
def matrix_multiplication(A, B):
# 矩阵A的行数和列数
row_a = len(A)
col_a = len(A[0])
# 矩阵B的行数和列数
row_b = len(B)
col_b = len(B[0])
# 初始化结果矩阵C
C = [[0 for j in range(col_b)] for i in range(row_a)]
# 矩阵乘法计算
for i in range(row_a):
for j in range(col_b):
for k in range(col_a):
C[i][j] += A[i][k] * B[k][j]
return C
# 打印结果矩阵
C = matrix_multiplication(A, B)
for row in C:
print(row)
```
输出结果为:
```
[30, 24, 18]
[84, 69, 54]
[138, 114, 90]
```
阅读全文