[SciPy-User] How to average different pieces or an array?

gilles Rochefort gilles.rochefort at gmail.com
Fri Aug 7 12:37:32 EDT 2009


Hi,

Not sure to answer well to the question (indeed there is loops)  but
have you tried
 something like this :

for s in [ slice(a,b) for a,b in zip(y[:-1],y[1:]) ]:
    x[s] = mean(x[s])

Regards,
Gilles Rochefort.
>       I'm sure this is easy I just can't think of how to do it without
> a bunch of for loops which I would like to avoid since they take up so
> much computational time.
>
> Say I have an array x, with 100 entries.   Lets say I have another
> array y that looks like this:
>
> y = [0,5,10,20,40,100]  which specified which ranges of x I would like
> to average.
>
> In other words, I want elements 0 - 4  in x to be replaced by their
> average value. 5-9 replaced with their average value. 10-19 replaced
> by their averaged value, etc...
>
>     Effectively this will give me a new array with one 100 entries
> with different sized binning along the array.
>
>     If anyone knows how to do this without using for loops the whole
> way I would appreciate it.
>
>      Example:  I will make it simpler in case my above description was
> confusing.  Say x = [1,3,2,6,7,4,5,4,9,4] and I passed in y =
> [0,2,4,10].  The above description would return a new X:
>
> X = [2,2,4,4,5.5,5.5,5.5,5.5,5.5,5.5]
>
>     So that the apprpriate bins are averaged.
>
>                                        Joseph Smidt
>   




More information about the SciPy-User mailing list