我需要检查一个点是否位于边界长方体内。长方体的数量非常大(~4M)。我想出的代码是:
import numpy as np
# set the numbers of points and cuboids
n_points = 64
n_cuboid = 4000000
# generate the test data
points = np.random.rand(1, 3, n_points)*512
cuboid_min = np.random.rand(n_cuboid, 3, 1)*512
cuboid_max = cuboid_min + np.random.rand(n_cuboid, 3, 1)*8
# main body: check if the points are inside the cuboids
inside_cuboid = np.all((points > cuboid_min) & (points < cuboid_max), axis=1)
indices = np.nonzero(inside_cuboid)
在我的电脑上运行需要 8 秒,运行需要 np.all3 秒。np.nonzero有什么想法可以加快代码速度吗?