定义一个函数计算两个2*2 矩阵更多乘法,乘法规则如图所示。输入两个矩阵,输出其乘法结果。示例运行结果如下。
时间: 2024-05-14 22:18:18 浏览: 102
以下是 Python 代码实现:
```python
def matrix_multiply(a, b):
c = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
c[i][j] += a[i][k] * b[k][j]
return c
a = [[1, 2], [3, 4]]
b = [[5, 6], [7, 8]]
c = matrix_multiply(a, b)
print("矩阵 A:")
for i in range(2):
print(a[i])
print("矩阵 B:")
for i in range(2):
print(b[i])
print("矩阵 C = A × B:")
for i in range(2):
print(c[i])
```
输出结果为:
```
矩阵 A:
[1, 2]
[3, 4]
矩阵 B:
[5, 6]
[7, 8]
矩阵 C = A × B:
[19, 22]
[43, 50]
```
阅读全文