使用np.instersect1d
之后,我已经检索了要检索的索引。我想将这些索引变成一个蒙版,然后再将其与另一个蒙版组合。
我想要的是:
>>> times_1.shape
... (160,)
>>> _, time_indices, _ = np.intersect1d(times_1, times_2, return_indices=True)
>>> time_indices.shape
... (145,)
# Some way to turn return mask from original shape and indices
>>> time_mask = get_mask_from_shape(time_1.shape, time_indices)
>>> time_mask.shape
... (160,)
实现功能get_mask_from_shape
的最简单方法是什么?
如果我有一个正确的想法,我将采用我一直在寻找few days before的这种模式:
intersection = np.intersect1d(times_1, times_2)
intersection = np.sort(intersection)
locations = np.searchsorted(intersection, times_1)
locations[locations==len(intersection)] = 0
time_mask = intersection[locations]==times_1
[searchsorted
方法根据times_1
]查找应将intersection
的元素插入到numpy
documentation.中以保持顺序的索引>
我找到的解决方案是: