[Numpy-discussion] Sum over array elements

Gökhan Sever gokhansever at gmail.com
Mon Apr 12 10:26:19 EDT 2010


On Mon, Apr 12, 2010 at 9:21 AM, Nicola Creati <ncreati at inogs.it> wrote:

> Hello,
> I want to calculate, given a one dimension array, the sum over every two
> elements of the array.
> I found this working solution:
>
> a = N.arange(10)
> b = a.reshape(a, (5, 2))
> c = b.sum(axis=1)
>
> Is there any better solution?
>
> Thanks,
>
> Nicola Creati
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

Sum odds and evens:

I[3]: d = a[1::2] + a[::2]

I[4]: d
O[4]: array([ 1,  5,  9, 13, 17])

-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100412/8524060c/attachment.html>


More information about the NumPy-Discussion mailing list