[Numpy-discussion] Any numpy trick for my problem ?

Stéfan van der Walt stefan at sun.ac.za
Wed Oct 15 07:05:33 EDT 2008


Hi Uwe

2008/10/15 Uwe Schmitt <rocksportrocker at googlemail.com>:
> I got a matrix of 2100 lines, and I want to calculate  blockwise mean
> vectors.
> Each block consists of 10 consecutive rows.
>
> My code looks like this:
>
>   rv = []
>   for i in range(0, 2100, 10):
>       rv.append( mean(matrix[i:i+10], axis=0))
>
>   return array(rv)
>
> Is there a more elegant and may be faster method to perform this
> calculation ?

You can use array striding.  See

http://mentat.za.net/numpy/numpy_advanced_slides/

from slide 29 onwards.

Regards
Stéfan



More information about the NumPy-Discussion mailing list