[Numpy-discussion] question about index array behavior

Russel Howe russel at appliedminds.com
Fri Jan 13 11:08:13 CST 2006


In the session below, I expected the for loop and the index array to  
have the same behavior.  Is this behavior by design?  Is there some  
other way to get the behavior of the for loop?  The loop is too slow  
for my application ( len(ar1) == 18000).
Russel

Python 2.4.2 (#1, Nov 29 2005, 08:43:33)
[GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> from numarray import *
 >>> import numarray.random_array as ra
 >>> print libnumarray.__version__
1.5.0
 >>> ar1=ra.random(10)
 >>> ar2=zeros(5, type=Float32)
 >>> ind=array([0,0,1,1,2,2,3,3,4,4])
 >>> ar2[ind]+=ar1
 >>> ar2
array([ 0.09791247,  0.26159889,  0.89386773,  0.32572687,   
0.86001897], type=Float32)
 >>> ar1
array([ 0.49895534,  0.09791247,  0.424059  ,  0.26159889,  0.29791802,
         0.89386773,  0.44290054,  0.32572687,  0.53337622,   
0.86001897])
 >>> ar2*=0.0
 >>> for x in xrange(len(ind)):
...     ar2[ind[x]]+=ar1[x]
...
 >>> ar2
array([ 0.5968678 ,  0.68565786,  1.19178581,  0.76862741,   
1.39339519], type=Float32)
 >>>





More information about the Numpy-discussion mailing list