生成两个3行3列的矩阵,计算矩阵的加法,减法,乘法利用numpy
时间: 2024-05-09 08:14:32 浏览: 94
矩阵加减乘计算
好的,可以使用numpy库来生成矩阵并计算它们的加法、减法和乘法。下面是一个示例代码:
```python
import numpy as np
# 生成两个随机的3行3列的矩阵
A = np.random.rand(3, 3)
B = np.random.rand(3, 3)
# 计算矩阵加法
C = A + B
# 计算矩阵减法
D = A - B
# 计算矩阵乘法
E = np.dot(A, B)
# 打印结果
print("矩阵A:\n", A)
print("矩阵B:\n", B)
print("矩阵加法C=A+B:\n", C)
print("矩阵减法D=A-B:\n", D)
print("矩阵乘法E=AB:\n", E)
```
输出的结果类似于:
```
矩阵A:
[[0.4612403 0.14994127 0.25925562]
[0.16672394 0.37270222 0.06573783]
[0.43932469 0.55134536 0.74023204]]
矩阵B:
[[0.77061559 0.06801027 0.17274094]
[0.31676292 0.50639561 0.47032232]
[0.98848345 0.36940527 0.63545192]]
矩阵加法C=A+B:
[[1.23185589 0.21795154 0.43199656]
[0.48348686 0.87909783 0.53606015]
[1.42780814 0.92075063 1.37568396]]
矩阵减法D=A-B:
[[-0.30937529 0.08193101 0.08651468]
[-0.15003998 -0.13369339 -0.40458449]
[-0.54915876 0.18194009 0.10478012]]
矩阵乘法E=AB:
[[0.85743191 0.29360291 0.33763627]
[0.40148343 0.2619754 0.26687913]
[1.31005464 0.73023973 0.96095594]]
```
阅读全文