Wednesday, 18 September 2013

Comparing two numpy arrays of different length

Comparing two numpy arrays of different length

I need to find the indices of the first less than or equal occurrence of
elements of one array in another array. One way that works is this:
import numpy
a = numpy.array([10,7,2,0])
b = numpy.array([10,9,8,7,6,5,4,3,2,1])
indices = [numpy.where(a<=x)[0][0] for x in b]
indices has the value [0, 1, 1, 1, 2, 2, 2, 2, 2, 3], which is what I
need. The problem of course, is that python "for" loop is slow and my
arrays might have millions of elements. Is there any numpy trick for this?
This doesn't work because they arrays are not of the same length:
indices = numpy.where(a<=b) #XXX: raises an exception
Thanks!

No comments:

Post a Comment