Skip to content
Advertisement

Replace all non-intersecting values with 0, between two numpy arrays

Say I have a multi-dimensional array, for example:

JavaScript

And say I have a one-dimensional array (b), with some values which may be seen in my multi-dimensional array (a):

JavaScript

Now I want to replace all of the non-intersecting values in array a with zero. I want to get the following result:

JavaScript

How could I achieve this?

I tried several ways of coding which didn’t work. I expected the following line to work, but I think there is an issue with using not in in this format:

JavaScript

I get the following error when I try this:

JavaScript

I am unsure what to try next. Any ideas?

Advertisement

Answer

np.isin() is perfect for this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement