comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Speed-up of code
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Speed-up of code [message #67866] Tue, 25 August 2009 07:16
philipelson is currently offline  philipelson
Messages: 9
Registered: March 2009
Junior Member
Dear All,

I have a question relating to the optimization of some code which
averages an array based on the values in another array.
Its much easier to explain in an example:

day = [ 1, 1, 2, 3, 3, 3, 3]
value = [ 2, 4, 5, 2, 3, 2, 1]

Which should return, depending on which is easier, either
avg = [ 3, 5, 2]
or
avg = [ 3, 3, 5, 2, 2, 2, 2]


This is fairly straightforward using a for loop, but how to do it in
the IDL way?

You can see two examples of the basic code below:


; ==================================
; FIRST EXAMPLE
; ==================================
unique = uniq(day)
avg = intarr(n_elements(unique))
FOR i=0, n_elements(unique) -1 DO BEGIN
res = WHERE(day EQ day[unique[i]], count)
if count GT 0 THEN avg[i] = total(value[res],/DOUBLE) / count
ENDFOR
print, avg

; ==================================
; SECOND EXAMPLE
; ==================================
h = histogram(day, REVERSE_INDICES=ri)
avg = h*0
FOR i=0, n_elements(h)-1 DO BEGIN
data_inds = ri[ri[i]:ri[i+1]-1]
avg[i] = total(value[data_inds],/DOUBLE) / h[i]
ENDFOR
print, avg


At this stage I open the floor; I essentially want to achieve the
results as above without the need for the for loop.

My assumption is that the HISTOGRAM function will be helpful, but
having spent quite some time on this I am beginning to think that it
cannot be done - though I would love to be proved wrong by any
histogram guru out there.

Thanks,

Philip
[Message index]
 
Read Message
Previous Topic: Help from UNIX IDL 7.1 Users
Next Topic: Re: Integer - decimal help

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 16:14:05 PDT 2025

Total time taken to generate the page: 0.00258 seconds