[Numpy-discussion] Numpy unexpected (for me) behaviour

V. Armando Sole sole at esrf.fr
Fri Jan 23 02:11:21 EST 2009


Hello,

In an effort to suppress for loops, I have arrived to the following situation.

Through vectorial logical operations I generate a set of indices for which 
the contents of an array have to be incremented. My problem can be reduced 
to the following:

#This works
import numpy
a=numpy.zeros(10)
b=numpy.ones(4, numpy.int)

for i in b:
     a[i] += 1
#a[1] contains 4 at the end


#This does not work
import numpy
a=numpy.zeros(10)
b=numpy.ones(4, numpy.int)
a[b] += 1

#a[1] contains 1 at the end

Is that a bug or a feature? Is there a way I can achieve the first result 
without a for loop? In my application the difference is a factor 10 in 
execution time (1000 secons instead of 100 ...)

Thanks,

Armando





More information about the NumPy-Discussion mailing list