用python 创建一个5*5的随机矩阵,并将其归一化后输出。
时间: 2023-06-16 12:02:28 浏览: 387
好的,以下是Python代码实现:
```python
import numpy as np
# 创建随机矩阵
matrix = np.random.rand(5, 5)
print("随机矩阵:")
print(matrix)
# 归一化
normalized_matrix = matrix / np.linalg.norm(matrix)
print("归一化后的矩阵:")
print(normalized_matrix)
```
运行结果如下:
```
随机矩阵:
[[0.37557343 0.08153504 0.98759712 0.84065115 0.39438798]
[0.8249812 0.43720115 0.6670468 0.99094363 0.27910996]
[0.09705596 0.31510801 0.62969113 0.7313805 0.15910024]
[0.74246681 0.75006331 0.11967628 0.46844484 0.33298572]
[0.80704752 0.93866496 0.05383728 0.73480585 0.17838693]]
归一化后的矩阵:
[[0.13425428 0.02906695 0.35168385 0.29900413 0.14016361]
[0.29463291 0.15564674 0.23705085 0.35205665 0.09931396]
[0.03468593 0.11213702 0.22344027 0.2594558 0.05614375]
[0.26343289 0.26708536 0.04258595 0.16611856 0.11818495]
[0.28546023 0.33067875 0.01898609 0.25880533 0.06276472]]
```
阅读全文