使用numpy获取布尔数组中不同长度范围的np.all()



我有以下numpy数组:

selected= [True, False, False,  True,  True,  True, True, True, False, False, False, True]
start_index = [0, 3, 5, 8, 10]
end_index = [3, 5, 8, 10, 12] # End index itself is not included in the defined ranges

我想得到以下结果:

result=[False, True,True,False,False]

换句话说,我想使用numpy:获得与此代码等效的代码

result=[]
for idx in range(0,5):
result.append(np.all(selected[start_index[idx]:end_index[idx]]))

困难在于,这些范围的长度不同,所以我不能只重塑selected数组并在每行上使用np.all((。

Mechanic Pig的答案正是我所需要的,将其发布在这里作为更好可见性的答案:

在我的情况下,不同的范围是selected阵列的连续切片,在这种特定情况下,这个答案就是我想要的:

np.logical_and.reduceat(selected, start_index)

最新更新