简体   繁体   English

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

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

Say I have a numpy array mask called m1 = [[False, True, False], [True, False, True]] And I want to find a mask m2 such that its (i,j) entry is True iff j >= 0 and m1[i, j+1] == True .假设我有一个名为m1 = [[False, True, False], [True, False, True]]的 numpy 数组掩码m1 = [[False, True, False], [True, False, True]]我想找到一个掩码 m2 ,使得它的 (i,j) 条目为 True iff j >= 0 and m1[i, j+1] == True Any elegant and efficient ideas as to how to pull that off?关于如何实现这一目标的任何优雅而有效的想法? Thanks谢谢

Here's a way slicing and using binary operators:这是一种切片和使用二元运算符的方法:

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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM