2

I have two numpy arrays: myarray and mask, which are both bitewise arrays(1s and 0s only).

What is the difference between

myarray[mask] = 0

and

myarray = np.where( mask, 0, myarray )

? Because I get different results and can't figure out why.

4
  • Can you provide examples of myarray and mask? Commented Oct 23, 2014 at 8:42
  • Not easy. Its a thresholded skeletonized image of 600x800 pixels. Commented Oct 23, 2014 at 8:45
  • 1
    Doesn't matter, make the data up and reduce it to 4X4 or something? Though I suspect John has answered your question anyway. Commented Oct 23, 2014 at 8:46
  • It appears to be what I was looking for. Thanx anyway. Commented Oct 23, 2014 at 8:52

1 Answer 1

3

Since you say mask contains 1's and 0's, the problem is that NumPy treats these as indexes, not as a mask. You probably want to make mask be of boolean type (True/False), in which case it can be the same length as myarray and will select those elements where mask is True.

np.where() always treats the first argument as a boolean array, so it probably does what you want already.

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.