我正在try 对点云麻木数组进行动态切片.切片将过滤x/y坐标落入由minxminymaxxmaxy定义的边界列表内的点

我以为我有一个有效的算法,但我面对的是The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

例如(这只是演示数据):

import numpy as np

bounds_list = [
  (0, 0, 5, 5),
  (8, 8, 10, 10)
]

#col0 --> x , col1 -->  y , col2 --> z
random_arr = np.array([10, 10, 100]) * np.random.rand(100, 3)


def filter_by_bounds(arr):
  slices = np.array([np.array([bounds[0] <= arr[0] <= bounds[2], bounds[1] <= arr[1] <= bounds[3]]).all() for bounds in bounds_list]).any()
  return arr[slices]


filtered_arr = filter_by_bounds(random_arr)
print(filtered_arr)

在此演示中,我希望检索其x/y坐标至少在一个界限内的所有点

推荐答案

您可以这样对其进行过滤:

def filter_by_bounds(arr):
    x = random_arr[:, 0]
    y = random_arr[:, 1]
    include = np.full(arr.shape[0], False)
    for bounds in bounds_list:
        include |= (bounds[0] <= x) & (x <= bounds[2]) & (bounds[1] <= y) & (y <= bounds[3])
    return arr[include]

这包括与某个界限匹配的任何点.它使用矢量化来搜索匹配点.

Python相关问答推荐

修剪Python框架中的尾随NaN值

查找3D数组中沿一个轴的相同值序列的长度(与行程长度编码相关)

为什么使用SciPy中的Distance. cos函数比直接执行其Python代码更快?

在Python中,什么表达相当于0x1.0p-53?

使用regex分析具有特定字符的字符串(如果它们存在)

Python plt.text中重叠,包adjust_text不起作用,如何修复?

在Pandas 日历中插入一行

在函数内部使用eval(),将函数的输入作为字符串的一部分

Python:在类对象内的字典中更改所有键的索引,而不是仅更改一个键

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何在虚拟Python环境中运行Python程序?

数据抓取失败:寻求帮助

Python,Fitting into a System of Equations

如何使用它?

如何获得每个组的时间戳差异?

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

关于Python异步编程的问题和使用await/await def关键字

旋转多边形而不改变内部空间关系

基于行条件计算(pandas)

从旋转的DF查询非NaN值