0

I want to compare numpy array two scale and data

for exaple

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

What I want to check is

Every positions where scale is 1 should be more than 0 in data

Is there any good numpy function to do this??

1
  • why not compare scale to (data2>0)? if (scale == (data2>0)).all(): print('OK'); else: print('NG') Commented Aug 3, 2021 at 10:42

1 Answer 1

2

Step by step using Numpy:

  1. Convert to boolean, will return true when value > 0
  2. Bitwise AND
  3. Check equality
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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you bitwise_and is quite new to me. and very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.