Changing boolean mask with another mask in python

I want to modify an existing mask for future use in my code according to some condition:

import pylab mask1 = arange(10) > 5; # [False False False False False False True True True True] mask2 = arange(10) >8; # [False False False False False False False False False True] mask1[mask2] = False print mask1 [ True False False False False False True True True True] 

As you can see, this was the first item that was changed, and not the last, as expected. What is the right way to do this?

EDIT: Sorry, my bad one, as some of you have indicated that the code is correct, I don’t know what is going on there, I will just delete the question.

+4
source share
2 answers

There is nothing wrong with the code. I tried this and it gives the correct result (which is different from the result you are showing).

Here is an alternative way to do the same:

 mask1 &= ~mask2 
+2
source

It seems to me that you can create a mask as follows:

 mask1= logical_and(arange(10)>5,arange(10)<=8) 

Or even simpler:

 mask1 = (arange(10)>5) & (arange(10)<=8) 
0
source

Source: https://habr.com/ru/post/1414396/


All Articles