I have a 1d array of ids, for example:
a = [1, 3, 4, 7, 9]
Then another 2d array:
b = [[1, 4, 7, 9], [3, 7, 9, 1]]
I would like to have a third array with the same shape of b where each item is the index of the corresponding item from a, that is:
c = [[0, 2, 3, 4], [1, 3, 4, 0]]
What's a vectorized way to do that using numpy?
np.searchsorted(a, b), IIUC. a needs to be sorted for this appraoch.lu = np.empty(max(a)+1, a.dtype); lu[a] = np.arange(len(a)); lu[np.array(b)]should be faster for larger arrays, but needs additional space for the look-up array.