python寻找矩阵每列最大值的坐标
时间: 2023-07-09 14:51:46 浏览: 92
可以使用numpy库中的argmax函数来寻找矩阵每列的最大值的索引,然后使用np.unravel_index函数将索引转换成对应的行列坐标。以下是示例代码:
```python
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
max_indices = np.argmax(matrix, axis=0) # 沿着列方向寻找最大值的索引
max_coords = np.unravel_index(max_indices, matrix.shape) # 将索引转换为坐标
print(max_coords)
```
在上述代码中,首先定义一个3x3的矩阵,然后使用argmax函数沿着列方向寻找最大值的索引,再使用np.unravel_index函数将索引转换为坐标,最后输出每列最大值的坐标。输出结果为:
```
(array([2, 2, 2], dtype=int64), array([2, 2, 2], dtype=int64))
```
其中,第一个数组为行坐标,第二个数组为列坐标,表示每列最大值的坐标。
相关问题
python 用二维移动窗口求二维矩阵的局部最大值的坐标
可以使用 scipy 库中的 signal 模块中的 argrelextrema 函数来实现。具体代码如下:
```python
import numpy as np
from scipy.signal import argrelextrema
def find_local_max(matrix):
# 定义移动窗口大小
window_size = 3
# 定义卷积核
kernel = np.ones((window_size, window_size))
# 对矩阵进行卷积操作
conv_matrix = np.abs(np.convolve(matrix, kernel, mode='same'))
# 找到局部最大值的坐标
max_indices = argrelextrema(conv_matrix, np.greater)[]
# 将一维坐标转换为二维坐标
row_indices, col_indices = np.unravel_index(max_indices, matrix.shape)
# 返回局部最大值的坐标
return list(zip(row_indices, col_indices))
```
使用示例:
```python
matrix = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
local_max = find_local_max(matrix)
print(local_max)
```
输出结果为:
```
[(2, 2)]
```
表示矩阵中的局部最大值为 13,坐标为 (2, 2)。
python 用二维移动窗口遍历求二维矩阵的局部最大值的坐标
可以使用numpy库中的函数来实现,具体代码如下:
```python
import numpy as np
def local_max(matrix, window_size):
# 定义移动窗口的步长
stride = 1
# 定义矩阵的行数和列数
rows, cols = matrix.shape
# 定义局部最大值的坐标列表
max_positions = []
# 遍历矩阵
for i in range(, rows - window_size + 1, stride):
for j in range(, cols - window_size + 1, stride):
# 获取当前窗口内的子矩阵
window = matrix[i:i+window_size, j:j+window_size]
# 判断子矩阵是否为局部最大值
if np.all(window == np.max(window)):
# 记录局部最大值的坐标
max_positions.append((i+window_size//2, j+window_size//2))
return max_positions
```
调用该函数,可以得到二维矩阵的局部最大值的坐标列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)