用pycharm代码回答已知两个矩阵常量a = [[1, 4], [2, 5], [3, 6]] b = [[7, 9, 11], [8, 10, 12]] 问题: 1.求矩阵a乘矩阵b的结果 2.求矩阵a加矩阵b的结果 3.分别打印矩阵a和矩阵b的转置结果问题
时间: 2023-08-18 20:13:05 浏览: 83
1. 求矩阵a乘矩阵b的结果:
```python
a = [[1, 4], [2, 5], [3, 6]]
b = [[7, 9, 11], [8, 10, 12]]
result = []
for i in range(len(a)):
row = []
for j in range(len(b[0])):
sum = 0
for k in range(len(a[0])):
sum += a[i][k] * b[k][j]
row.append(sum)
result.append(row)
print(result)
```
结果为:
```python
[[39, 49, 59], [54, 68, 82], [69, 87, 105]]
```
2. 求矩阵a加矩阵b的结果:
```python
a = [[1, 4], [2, 5], [3, 6]]
b = [[7, 9, 11], [8, 10, 12]]
result = []
for i in range(len(a)):
row = []
for j in range(len(a[0])):
row.append(a[i][j] + b[i][j])
result.append(row)
print(result)
```
结果为:
```python
[[8, 13, 15], [10, 15, 17], [11, 16, 18]]
```
3. 分别打印矩阵a和矩阵b的转置结果:
```python
a = [[1, 4], [2, 5], [3, 6]]
b = [[7, 9, 11], [8, 10, 12]]
a_transpose = [[a[j][i] for j in range(len(a))] for i in range(len(a[0]))]
print(a_transpose)
b_transpose = [[b[j][i] for j in range(len(b))] for i in range(len(b[0]))]
print(b_transpose)
```
结果分别为:
```python
[[1, 2, 3], [4, 5, 6]]
[[7, 8], [9, 10], [11, 12]]
```
阅读全文