python有一个4×5的矩阵(可为随机矩阵),编程求出其最大值及其所处的位置
时间: 2024-02-16 20:02:54 浏览: 23
好的,以下是Python的代码实现:
```python
import numpy as np
# 生成随机矩阵
matrix = np.random.rand(4, 5)
# 找到最大值及其位置
max_value = matrix.max()
max_index = np.unravel_index(matrix.argmax(), matrix.shape)
# 输出结果
print("矩阵为:\n", matrix)
print("最大值为:", max_value)
print("最大值所在位置为:", max_index)
```
输出结果类似于:
```
矩阵为:
[[0.56875789 0.46598339 0.38899683 0.82032228 0.32881311]
[0.96277069 0.91669514 0.60542428 0.2587265 0.08505421]
[0.0925067 0.40669501 0.07375638 0.46722198 0.30977664]
[0.84014494 0.74740703 0.37638759 0.60271747 0.26756191]]
最大值为: 0.9627706894412243
最大值所在位置为: (1, 0)
```
其中,`np.random.rand(4, 5)` 生成一个 $4\times5$ 的随机矩阵,`matrix.max()` 找到矩阵中的最大值,`np.unravel_index(matrix.argmax(), matrix.shape)` 找到最大值在矩阵中的位置。