1

I have a smaller array as:

A = np.array([2013, 2014, 2015])
Aval = np.array([14, 10, 35])

I have another array:

A2 = np.array([2013, 2014, 2015, 2013, 2014, 2015, 2013, 2013, 2013])

I want to create A2val such that:

Arval = np.array([14, 10, 35, 14, 10, 35, 14, 14, 14])

so, I am trying to use the values in array A to map to elements of A2 and generate an extended version of A2val

1 Answer 1

2

You can use np.searchsorted to create the mapping indices and then index into Aval for selecting elements off it, like so -

Aval[np.searchsorted(A,A2)]

If A is not already sorted, we need to use the optional argument sorter, like so -

Aval[np.searchsorted(A,A2,sorter=A.argsort())]
Sign up to request clarification or add additional context in comments.

Comments

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.