比较Numpy数组,如位掩码



我想比较numpy数组两个scaledata

的做法

scale = [1,0,0,0,1,0,0,0,1,0,0,0] first and fifth and ninth bit is 1
data1 = [8,2,0,1,0,0,1,0,1,0,0,0] -> NG, because fifth bit is `0`
data2 = [8,0,0,0,1,0,1,0,1,0,0,0] -> OK, because first ,fifth, ninth bit is more than 0

我要检查的是

datascale1的位置应大于0

是否有任何好的numpy函数来做到这一点??

一步一步使用Numpy:

  1. 转换为布尔值,当value>0
  2. 位和
  3. 检查平等
import numpy as np
scale = [1,0,0,0,1,0,0,0,1,0,0,0]
data1 = [8,2,0,1,0,0,1,0,1,0,0,0]
data2 = [8,0,0,0,1,0,1,0,1,0,0,0]
scale= np.array(scale, dtype=bool)
data1= np.array(data1, dtype=bool)
data2= np.array(data2, dtype=bool)
and1 = np.bitwise_and(scale, data1)
and2 = np.bitwise_and(scale, data2)
is_match1 = np.array_equal(scale, and1)
is_match2 = np.array_equal(scale, and2)
print(is_match1) # False
print(is_match2) # True

最新更新