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

Home » Public Forums » archive » Re: averaging over same index
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: averaging over same index [message #45291 is a reply to message #45290] Tue, 23 August 2005 09:41 Go to previous messageGo to previous message
K. Bowman is currently offline  K. Bowman
Messages: 330
Registered: May 2000
Senior Member
In article <1124814140.855435.78130@f14g2000cwb.googlegroups.com>,
andi.walther@web.de wrote:

> Hello,
>
> I have an array V(alues) and an array S(ubscripts of a target array)
> and I want to
> extract the mean of all values with the same index in order to put them
> into the new array.
>
> As a simple example:
> V = [ 3, 7, 99, 5, 2 , 10]
> S = [ 1, 3, 3, 2 , 0 ,1]
>
> new vector should be --> new = [ 2 , 6.5 , 5, 53 ]
>
> slow way would be: for n = 0 , max(S)-1 do new[n]=mean(v[where(S eq
> n)])
>
> a bit faster without WHERE in the loop:
>
> VSorted = v[sort(S)]
> SSorted = S[sort(S)]
> uu = uniq(iSorted)
> for n = 0 , n_elements(uu)-1 do new[iSorted[uu[n]]] =
> mean(vSorted[n:(n-1)>0])
>
> But is there a way to do what I want without resorting to
> a loop? In my real-world problem I have vectors with 500000 elements
> and the number of the occurence of indices
> are quite irragular and can exceed 30 times.
>
> Thanks Andi

Use HISTOGRAM with reverse indices to get lists of each s. Then average using
array subscripting for each list.

See JD's HISTOGRAM tutorial on David's web site.

http://dfanning.com/tips/histogram_tutorial.html

Ken Bowman
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: averaging over same index
Next Topic: Different FFT times for same array size ?

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

Current Time: Wed Oct 08 14:10:22 PDT 2025

Total time taken to generate the page: 0.00412 seconds