Tricky data-summing question [message #43934] |
Tue, 03 May 2005 13:27 |
M. Katz
Messages: 69 Registered: May 2005
|
Member |
|
|
Here's a tricky question I've been wrestling with, in need of
inspiration.
I've got an array of raw data that I want to downsample into an output
array, but in a special way. I have an array of index values telling me
where to put the raw input for summation. It looks like this
in = [0.1,0.2,0.3, 1, 2, 10,20]
w = [ 0, 0, 0, 1, 2, 3, 3]
out = fltarr(5)
; The idea is that, following w, the first three elements of in[] need
to be summed into out(0). Also, out[3] will contain the sum of 10 and
20.
; This slow code does what we want, but inelegantly.
for i=0,n_elements(in)-1 do out(w(i)) += in(i)
out = [0.6, 1., 2., 30., 0.] ; this is the right answer
In practice in[] and w[] have thousands of elements. I'm wondering if
there's a quick, clever trick with histogram or something that would
make this calculation fly.
Thanks,
M. Katz
|
|
|