两个排序的部分重叠的 numpy 数组之间的索引映射

Index mapping between two sorted partially overlapping numpy arrays

我想解决类似于 中详述的问题,但是这两个数组不一定包含同一组值,尽管它们的值在每个数组中是唯一的,并且已排序。

例如如果我有两个数组:

a = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
b = np.array([2.2, 3.0, 4.4, 6.0])

我想得到一个与 a 长度相同的数组,它给出匹配元素所在的 b 的索引,如果没有匹配则为 -1。 IE。在这种情况下:

map = np.array([-1, 0, -1, 2, -1])

是否有使用 np.searchsorted 的简洁、快速的方法?

使用搜索排序索引检查匹配项,然后使用无效说明符屏蔽无效项。对于匹配检查,请执行 b[idx]==aidx 作为这些索引。因此-

invalid_specifier = -1
idx = np.searchsorted(b,a)
idx[idx==len(b)] = 0
out = np.where(b[idx]==a, idx, invalid_specifier)