在 2D numpy 矩阵中查找特定点距离 1 内的所有点
Find all points within distance 1 of specific point in 2D numpy matrix
我想找到在我的 numpy 矩阵中某个点的范围 1(或完全对角线)内的点列表:
例如说我的矩阵 m
是:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 1 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
我想获得一个元组列表或表示下面带有 X 的 9 个点的所有坐标的东西:
[[0 0 0 0 0]
[0 X X X 0]
[0 X X X 0]
[0 X X X 0]
[0 0 0 0 0]]
这是目标点在边缘的另一个例子:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 1]
[0 0 0 0 0]
[0 0 0 0 0]]
在这种情况下,目标点的距离 1 内只有 6 个点:
[[0 0 0 0 0]
[0 0 0 X X]
[0 0 0 X X]
[0 0 0 X X]
[0 0 0 0 0]]
编辑:
这里使用 David Herrings answer/comment 关于切比雪夫距离是我尝试解决上面的示例 2 假设我知道目标点的坐标:
from scipy.spatial import distance
point = [2, 4]
valid_points = []
for x in range(5):
for y in range(5):
if(distance.chebyshev(point, [x,y]) <= 1):
valid_points.append([x,y])
print(valid_points) # [[1, 3], [1, 4], [2, 3], [2, 4], [3, 3], [3, 4]]
对于更大的数组,这似乎有点低效,因为我只需要检查一小部分单元格,而不是整个矩阵。
这里没有感兴趣的算法。如果您还知道 1 在哪里,首先您必须找到它,没有比搜索每个元素更好的方法了。 (你 可以 通过让 numpy
以 C 速度和 argmax
执行此操作来获得常数因子加速;使用 divmod
将展平索引分离为行和列。)此后,您所做的就是将 ±1(或 0)添加到坐标中,除非它会将您带到数组边界之外。您永远不会构造坐标只是为了稍后丢弃它们。
我觉得你让它有点太复杂了——不需要依赖复杂的函数
import numpy as np
# set up matrix
x = np.zeros((5,5))
# add a single point
x[2,-1] = 1
# get coordinates of point as array
r, c = np.where(x)
# convert to python scalars
r = r[0]
c = c[0]
# get boundaries of array
m, n = x.shape
coords = []
# loop over possible locations
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
# check if location is within boundary
if 0 <= r + i < m and 0 <= c + j < n:
coords.append((r + i, c + j))
print(coords)
>>> [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
一个简单的方法是用笛卡尔积得到所有可能的坐标
设置数据:
x = np.array([[0,0,0], [0,1,0], [0,0,0]])
x
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
您知道坐标将是您所在位置的 +/- 1:
loc = np.argwhere(x == 1)[0] # unless already known or pre-specified
v = [loc[0], loc[0]-1, loc[0]+1]
h = [loc[1], loc[1]-1, loc[1]+1]
output = []
for i in itertools.product(v, h):
if not np.any(np.array(i) >= x.shape[0]) and not np.any(np.array(i) < 0): output.append(i)
print(output)
[(1, 1), (1, 0), (1, 2), (0, 1), (0, 0), (0, 2), (2, 1), (2, 0), (2, 2)]
我想找到在我的 numpy 矩阵中某个点的范围 1(或完全对角线)内的点列表:
例如说我的矩阵 m
是:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 1 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
我想获得一个元组列表或表示下面带有 X 的 9 个点的所有坐标的东西:
[[0 0 0 0 0]
[0 X X X 0]
[0 X X X 0]
[0 X X X 0]
[0 0 0 0 0]]
这是目标点在边缘的另一个例子:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 1]
[0 0 0 0 0]
[0 0 0 0 0]]
在这种情况下,目标点的距离 1 内只有 6 个点:
[[0 0 0 0 0]
[0 0 0 X X]
[0 0 0 X X]
[0 0 0 X X]
[0 0 0 0 0]]
编辑:
这里使用 David Herrings answer/comment 关于切比雪夫距离是我尝试解决上面的示例 2 假设我知道目标点的坐标:
from scipy.spatial import distance
point = [2, 4]
valid_points = []
for x in range(5):
for y in range(5):
if(distance.chebyshev(point, [x,y]) <= 1):
valid_points.append([x,y])
print(valid_points) # [[1, 3], [1, 4], [2, 3], [2, 4], [3, 3], [3, 4]]
对于更大的数组,这似乎有点低效,因为我只需要检查一小部分单元格,而不是整个矩阵。
这里没有感兴趣的算法。如果您还知道 1 在哪里,首先您必须找到它,没有比搜索每个元素更好的方法了。 (你 可以 通过让 numpy
以 C 速度和 argmax
执行此操作来获得常数因子加速;使用 divmod
将展平索引分离为行和列。)此后,您所做的就是将 ±1(或 0)添加到坐标中,除非它会将您带到数组边界之外。您永远不会构造坐标只是为了稍后丢弃它们。
我觉得你让它有点太复杂了——不需要依赖复杂的函数
import numpy as np
# set up matrix
x = np.zeros((5,5))
# add a single point
x[2,-1] = 1
# get coordinates of point as array
r, c = np.where(x)
# convert to python scalars
r = r[0]
c = c[0]
# get boundaries of array
m, n = x.shape
coords = []
# loop over possible locations
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
# check if location is within boundary
if 0 <= r + i < m and 0 <= c + j < n:
coords.append((r + i, c + j))
print(coords)
>>> [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
一个简单的方法是用笛卡尔积得到所有可能的坐标
设置数据:
x = np.array([[0,0,0], [0,1,0], [0,0,0]])
x
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
您知道坐标将是您所在位置的 +/- 1:
loc = np.argwhere(x == 1)[0] # unless already known or pre-specified
v = [loc[0], loc[0]-1, loc[0]+1]
h = [loc[1], loc[1]-1, loc[1]+1]
output = []
for i in itertools.product(v, h):
if not np.any(np.array(i) >= x.shape[0]) and not np.any(np.array(i) < 0): output.append(i)
print(output)
[(1, 1), (1, 0), (1, 2), (0, 1), (0, 0), (0, 2), (2, 1), (2, 0), (2, 2)]