【问题标题】:Efficiently find all points within sorted 2D Numpy Array有效地找到排序的 2D Numpy 数组中的所有点
【发布时间】:2021-11-11 06:58:05
【问题描述】:

如何从等距点的排序 numpy 数组中有效地找到给定半径和中心的圆内的点集?

例如,这是我的代码以及我目前如何在半径内提取这些点。

import numpy as np
n_points = 10000
x_lim = [0, 100]
y_lim = [0, 100]
x, y = np.meshgrid(np.linspace(*x_lim, n_points), np.linspace(*y_lim, n_points))
xy = np.vstack((x.flatten(), y.flatten())).T 
# Current approach
radius = 5
point = np.array([50, 35], dtype=float) 
# Indexes of those points within a circle of radius centered at point
idxs = np.linalg.norm(point - xy, axis=-1) < radius
points_within_circle = xy[idxs]

如何更有效地计算这些索引?我想是因为数组是结构化的并且在每个点之间有一个固定的距离,我应该能够利用它来消除大部分检查。

【问题讨论】:

标签: python arrays numpy


【解决方案1】:

人们忘记的最重要的技巧之一是计算distance**2 并将其与radius**2 进行比较比计算distance &lt; radius 快得多。因此,假设您使用的是 0 的中心,请计算 x**2 + y**2,并与 25 进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2012-11-14
    • 2015-01-15
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多