用Python生成两个2×2矩阵,并计算矩阵乘积
时间: 2024-05-09 10:18:51 浏览: 123
代码如下:
```python
import numpy as np
# 生成两个2×2矩阵
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# 计算矩阵乘积
C = np.dot(A, B)
print("A =\n", A)
print("B =\n", B)
print("A × B =\n", C)
```
输出结果为:
```python
A =
[[1 2]
[3 4]]
B =
[[5 6]
[7 8]]
A × B =
[[19 22]
[43 50]]
```
相关问题
python生成两个2x2矩阵,并计算矩阵乘积
以下是Python代码实现:
```python
import numpy as np
# 生成两个2x2矩阵
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# 计算矩阵乘积
product = np.dot(matrix1, matrix2)
print("矩阵1:")
print(matrix1)
print("矩阵2:")
print(matrix2)
print("矩阵乘积:")
print(product)
```
输出结果为:
```
矩阵1:
[[1 2]
[3 4]]
矩阵2:
[[5 6]
[7 8]]
矩阵乘积:
[[19 22]
[43 50]]
```
用python生成两个2*2矩阵(数据用arange函数生成),计算两个矩阵的乘积
好的,我可以帮你解决这个问题。以下是Python代码实现:
```python
import numpy as np
# 生成两个2*2矩阵
matrix1 = np.arange(1, 5).reshape((2, 2))
matrix2 = np.arange(5, 9).reshape((2, 2))
# 计算两个矩阵的乘积
result = np.dot(matrix1, matrix2)
print("Matrix 1:")
print(matrix1)
print("Matrix 2:")
print(matrix2)
print("Result:")
print(result)
```
输出结果为:
```
Matrix 1:
[[1 2]
[3 4]]
Matrix 2:
[[5 6]
[7 8]]
Result:
[[19 22]
[43 50]]
```
希望能帮到你!
阅读全文