import numpy as np
a=np.array([[4,2,6],[3,6,5]])
b=np.array([3,5])
I want to update the numbers in "a" which are bigger than the numbers in "b" to np.nan. If they are smaller or equal i don't want it to be changed. I want to compare the first row of "a" to the first scalar of "b" and the second row of "a" to the second scalar of "b".
eg
a = array([[4, 2, 6],
[3, 6, 5]])
the updated value should be:
array([[nan, 2, nan],
[3, nan, 5]])
I've tried this:
for i in range(2):
a[i]=np.where(a[i]<=b[i],a[i],np.nan)
But it doesn't work. HELP ME PLEASE!!
You can write so:
import numpy as np
a=np.array([[4,2,6],[3,6,5]])
b=np.array([3,5])
# shape in compared axis must be the same or one of their length must be equal 1
# in this case their shape is b(2,1) and a(2,3)
a = np.where(a <= b.reshape(b.shape[0],1), a, np.nan)
print(a)
but in more difficult cases I'm not sure, that it will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.