1

I have two arrays:

array1 =    [[3,7,2],
             [1,4,5],
             [9,8,7]]

array2 =    [[0,1,0],
             [1,0,0],
             [1,0,1]]

I like to get numpy.sum(array1), but only where array2==0

So in the end I like to have a value of 22 (3+2+4+5+8).

1 Answer 1

2
array1 = numpy.array([[3,7,2],
                      [1,4,5],
                      [9,8,7]])

array2 = numpy.array([[0,1,0],
                      [1,0,0],
                      [1,0,1]])

result = numpy.sum(array1[array2==0])
print(result)

Output: 22

You can index numpy arrays with other numpy arrays. Many operators, like ==, can be applied as element-wise operations to numpy arrays.

Sign up to request clarification or add additional context in comments.

Comments

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.