0

I want to solve something like the problem detailed at Find index mapping between two numpy arrays, but where the two arrays do not necessarily contain the same set of values, although their values are unique within each array, and are sorted.

E.g. if I have two arrays:

a = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
b = np.array([2.2, 3.0, 4.4, 6.0])

I want to get an array of the same length as a which gives the index into b where the matching element is, or -1 if there is no match. I.e. in this case:

map = np.array([-1, 0, -1, 2, -1])

Is there a neat, fast way to do this using np.searchsorted?

1 Answer 1

2

Use the searchsorted indices to do a check on matches and then mask the invalid ones with the invalid-specifier. For the matching check, do b[idx]==a with idx as those indices. Hence -

invalid_specifier = -1
idx = np.searchsorted(b,a)
idx[idx==len(b)] = 0
out = np.where(b[idx]==a, idx, invalid_specifier)
Sign up to request clarification or add additional context in comments.

2 Comments

I want to check why this works. Am I right in saying that you are setting the overflowed index values to 0 (idx[idx==len(b)] = 0), because you are guaranteed, if the array is sorted, that b[0] are guaranteed never to equal the values of a when they are at the far right of the array?
@user2667066 I know that those overflowed ones aren't the matches.So, it doesn't matter what we assign to those in idx as there are no corresponding matches in a So, that setting is just to make sure the next step indexing doesn't throw exception because of out of bounds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.