How to use numpy.where on non-broadcastable arrays?

23 hours ago 4
ARTICLE AD BOX

Suppose I have four arrays a,b,c and d, where a,b and c have the same shape (n,), but the shape of d may be completely different. I would like to do something like

np.where(a < d, b, c)

and would like this to give me an array e of shape (n,)+d.shape such that e[i,...] = b[i] if d[...] > a[i] and d[i,...] = c[i] if d[...] <= a[i] .

The above code in general does not work, as the arrays may not be broadcastable. How could one write it cleanly?

Read Entire Article