I've been Googling this for a while now, even though I think it's a common problem, I don't see there's a solution anywhere on SO.
Say I have an array of 3D vectors (x, y, z), like this:
import numpy as np
arr = np.array(
[(1, 2, 3), (3, 1, 2.5), (5, 3, 1), (0, -1, 2)],
dtype=[('x', np.float), ('y', np.float), ('z', np.float)]
)
print(np.sort(arr, order='z'))
This prints:
[(5., 3., 1. ) (0., -1., 2. ) (3., 1., 2.5) (1., 2., 3. )]
I would like to now search this sorted array, by dimension 'z' only. A binary search would be extremely efficient. But searchsorted only works on 1D arrays. And there's no lambda you can apply to each value (basically np.dot with a (0, 0, 1) vector.)
Is there any method to do this in numpy or do I need to implement binary search myself (still an option since it's very fast even in vanilla Python).
For example for value x= 2.5 I'd expect the index 2. And for x=2.4 I'd still expect 2, for x=2.6 I'd expect 3. Either the index or the vector itself (like (3, 1, 2.5)).