我有以下数组
I0 = np.array([1, 0, 0, 0, 1, 0, 0, 1])
X0 = np.array([1, 3, 4, 4, 5, 6, 7, 8])
I1 = np.array([1, 0, 0, 1, 1, 0, 1])
X1 = np.array([1, 4, 5, 6, 7, 8, 9])对于X1 where I1 == 1中的值,我希望查找X0 where (I0 == 1) & (X0 <= X1)中的索引
indices = np.searchsorted(X0[I0 == 1], X1[I1 == 1], side='right')-1
X0[I0 == 1][indices] # [1, 5, 5, 8]但是我想要索引到X0,而不是索引到X0[I0 == 1]。
发布于 2019-05-16 15:14:25
对应元素的索引为
Q0 = np.arange(X0.size)[I0 == 0]所以
indices = Q0[indices]另外,我强烈建议您将I*数组设置为布尔值。例如:
I0 = np.array([True, False, False, False, True, False, False, True])这将允许您直接使用它进行索引,而不必创建另一个临时数组:
X0[I0]https://stackoverflow.com/questions/56162683
复制相似问题