[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can I de-loop?



I have a suggestion for the first of your quandries:

> a) I am adding a constant amount K to several elements N of array A.
> The elements Ni are not unique; if an Ni is repeated, I want to add it
> to A each time.  For instance, given
> 
> A = [0,1,2]
> N = [2,2,2]
> K = 4
> 
> I want the result A = [0,1,14], not A = [0,1,6]

IDL> A[min(N):max(N)] = A[min(N):max(N)] + K*histogram(N)
IDL> print,A
       0       1      14

This seems to be at least twice as fast as the loop with the time
difference increasing as the number of elements in A and N increase, at
least for the values I played around with ( up to 100,000 elements).

You should check this thread later as I should point out that over 95%
of my suggestions to this newsgroup do not end up helping the original
question.  Either my solution is too specific for the problem or someone
else soon posts a better solution.  I hope for the latter as I can also
learn something that way.

Cheers,
eddie

----- ---- --- ---  ---- --- --  --- --- --  -- - -  -  -
|\               A G Edward Haskell              
|\    Center for Coastal Physical Oceanography    
|\   Old Dominion University, Norfolk VA  23529   
|\     Voice 757.683.4816    Fax 757.683.5550     
|\          e-mail  haskell*ccpo.odu.edu          
----- ---- --- ----  --- --- ---  --- -- --  -- - -  -   -