使用相同形状的现有掩码来掩蔽 numpy 数组

masking a numpy array using an existing mask of the same shape

假设我有一个名为 m1 = [[False, True, False], [True, False, True]] 我想找到一个掩码 m2 使其 (i,j) 条目为真当且仅当 j >= 0 and m1[i, j+1] == True。 关于如何实现这一目标的任何优雅而有效的想法? 谢谢

这是一种切片和使用二元运算符的方法:

m1 = np.array([[False, True, False], [True, False, True]])

m2 = np.full(m1.shape, False)
m2[:, :-1] = m1[:, 1:] | m2[:, :-1]

print(m2)

array([[ True, False, False],
       [False,  True, False]])