I need to use searchsorted() along the first axis of an n dimensional array against an n-1 dimensional array. This is illustrated for a 3 dimensional array:
def find(x,y):
rows = np.zeros(y.shape, dtype=np.int)
for ii in range(y.shape[0]):
for jj in range(y.shape[1]):
rows[ii, jj] = np.searchsorted(x[:, ii, jj], y[ii, jj])
return rows
np.random.seed(42)
h = np.random.random((10,2,10))
h.sort(axis=0)
f = np.random.random((2,10))
print(find(h,f))
result is something like this:
[[10 0 3 10 5 0 1 5 0 6]
[ 7 9 2 1 10 1 3 9 5 10]]
Obviously using those loops is probably slow and this solution does not extend to n dimensions. I am concerned with speed and searchsorted() is very fast on sorted arrays when compared to things like argmax() and where().
Is there some way to apply searchsorted() along an axis like this without looping? The utility apply_along_axis kind of works, but makes a big array in 2 dimensions that you need to grab the diagonal of, which seems like a waste. I can't tell what it is doing in more than 2 dimensions.