Re: Tricky data-summing question [message #43929] |
Tue, 03 May 2005 13:50 |
Sean Davis
Messages: 19 Registered: August 1999
|
Junior Member |
|
|
M. Katz wrote:
> 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
If you use histogram and the REVERSE_IND keyword, you could perform this
operation, and you would only have to sum over each bin in the histogram,
which i presume would be a smaller number than summing over n_elements(in).
-Sean
|
|
|