Q:

How to update np.array conditionally?

I got two numpy arrays:
Out[23]: a = np.array([3, 7, 8, 9, 2], dtype=float)
Out[24]: b = np.array([5, 6, 7, 10, 3], dtype=float)

I need to compare a and b, and update array b so that its elements will contain
the higher value or np.nan
Example code:
for i in range(len(b)):
    b[i] = b[i] if b[i] > a[i] else np.nan

Result:
b
Out[33]: array([  5.,  nan,  nan,  10.,   3.])

Is there a way to do this without using a for loop?
Thanks in advance.

A:

You can use np.where
>>> np.where(b > a, b, np.nan)
array([  5.,  nan,  nan,  10.,   3.])

