Indexes of elements

Question:

I have numpy array:
A = np.array([1,3,5,7,9])
and
B = np.array([3,3,3,5,5,5])

I want to have array C – which is index B in A.
C = np.array([1,1,1,2,2,2])

How I can do it?

Asked By: AlexM

||

Answers:

The function searchsorted provides exactly the functionality you need.

C = np.searchsorted(A, B)
Answered By: 7shoe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.