Benjamin Lee
Benjamin Lee

Reputation: 501

For each element in one sorted numpy array, how to subtract the smallest greater value in another sorted array

Let's say we have two sorted numpy arrays, a and b.

a = np.array([ 0,  1,  2, 10])
b = np.array([ 7, 13])

For each element array a, I want to subtract the first larger element in b to get something like this:

>>> f(a, b)
array([-7, -6, -5, -3])

I can do this with an inefficient for loop, but is there a more numpythonic way to do it?

Upvotes: 3

Views: 330

Answers (2)

Alain T.
Alain T.

Reputation: 42143

The title and explanations are inconsistent.

Assuming that you're looking for the first element of b that is larger, not the smallest of b that is larger, then this will do it:

a - b[np.argmax(a[:,None]<b,axis=1)]

# array([-7, -6, -5, -3])

If you do need the smallest that is larger, you could sort b beforehand using b = np.sort(b) but then, using searchsorted(), as proposed by Mark Meyer, will be more efficient.

note that you must have at least one element in b that is larger than the largest element of a

Upvotes: 0

Mark
Mark

Reputation: 92440

You could use searchsorted for this. It will require that b is sorted and that a doesn't have values greater than the largest in b.

> a = np.array([0, 1, 2, 10, 12, 5, 7])
> b = np.array([7, 13])
> a - b[np.searchsorted(b, a, side='right')]

array([-7, -6, -5, -3, -1, -2, -6])

Upvotes: 4

Related Questions