How can I mask two NumPy arrays properly? I want find pe values that are not equal to 255, for example. I also want my desired output array be the same size aspd and pe, i.e., (7, 7) and filled with 0's.
What is the most efficient way to achieve this?
import numpy as np
pd = np.random.randint(254,256, size=(7,7))
pe = np.random.randint(0,7, size=(7,7))
Desired output
[[6 6 0 0 6 6 1]
[2 6 1 1 5 6 3]
[3 4 6 6 3 5 6]
[3 5 0 3 2 0 0]
[0 3 6 1 3 6 1]
[6 3 4 1 0 3 1]
[6 0 4 2 2 6 4]]
Many thanks
pe[index] if pd[index] != 255 else 0, right?pe * (pd == 255), IIUC. Assuming you meant that are not equal to 255 in pdnp.where(pd == 255, 0, pe)which I was going to propose. Kudos! :)np.wherein the very last version (1.23) of Numpy sonp.where(pd == 255, 0, pe)may now be faster thanpe * (pd == 255)or at least close to that.