python编写代码实现:①创建一个3*3并且值从0到8的矩阵,并将其进行转置操作②创建一个10*10的随机数组并找到它的最大值
时间: 2024-03-12 21:48:09 浏览: 207
(简单的编写方案)编写一个程序,生成一个10*10的随机矩阵并保存为文件(空格分隔行向量、换行分割列向量),再写程序将刚才保存的矩阵文件另存为CSV格式,用Excel或文本编辑器查看结果
5星 · 资源好评率100%
实现如下:
①
```python
import numpy as np
# 创建一个3*3并且值从0到8的矩阵
arr = np.arange(9).reshape(3,3)
print("原始矩阵:")
print(arr)
# 进行转置操作
arr_transpose = arr.T
print("转置后矩阵:")
print(arr_transpose)
```
输出:
```
原始矩阵:
[[0 1 2]
[3 4 5]
[6 7 8]]
转置后矩阵:
[[0 3 6]
[1 4 7]
[2 5 8]]
```
②
```python
import numpy as np
# 创建一个10*10的随机数组
arr = np.random.rand(10,10)
print("随机数组:")
print(arr)
# 找到最大值
max_value = arr.max()
print("最大值为:", max_value)
```
输出:
```
随机数组:
[[0.70832044 0.10640714 0.24951811 0.77918189 0.83607686 0.49892394
0.69919808 0.72630498 0.82021317 0.94108454]
[0.69158859 0.10944228 0.79993521 0.61879458 0.41480937 0.23485734
0.88231768 0.8917969 0.45688826 0.87134807]
[0.03957916 0.42758501 0.04083683 0.09425034 0.86938692 0.79477581
0.98136471 0.09014205 0.89856962 0.48570117]
[0.28118978 0.99149924 0.0441191 0.8417489 0.89200659 0.25446053
0.25791051 0.6079695 0.13956109 0.72064392]
[0.19068448 0.10644696 0.34595284 0.33014839 0.77852877 0.19273168
0.50850338 0.77311749 0.33689083 0.75462393]
[0.40042912 0.14526892 0.54734348 0.37790533 0.00388888 0.99730998
0.33755174 0.12465108 0.60702384 0.03960081]
[0.76674045 0.23392255 0.35606271 0.63404451 0.99199098 0.564425
0.74431549 0.15913936 0.76913655 0.98862223]
[0.01625515 0.03823404 0.8122335 0.51558577 0.20135262 0.62166114
0.87648045 0.36491187 0.49575125 0.01261471]
[0.11630303 0.70792235 0.25567863 0.32493578 0.16383299 0.19229176
0.10067288 0.80820497 0.31252726 0.89925505]
[0.06636632 0.14565764 0.72213888 0.0976316 0.49930727 0.63089353
0.08685184 0.94919559 0.45496229 0.11809259]]
最大值为: 0.9973099761368365
```
阅读全文