[Numpy-discussion] new question - summing a list of arrays

Keith Goodman kwgoodman@gmail....
Tue Mar 18 10:23:12 CDT 2008


On Tue, Mar 18, 2008 at 8:12 AM, Chris Withers <chris@simplistix.co.uk> wrote:
> Hi All,
>
>  Say I have an aribtary number of arrays:
>
>  arrays = [array([1,2,3]),array([4,5,6]),array([7,8,9])]
>
>  How can I sum these all together?
>
>  My only solution so far is this:
>
>  sum = arrays[0]
>  for a in arrays[1:]:
>    sum += a
>
>  ...which is ugly :-S

>> import numpy.matlib as M
>> x=[M.rand(3,1), M.rand(3,1), M.rand(3,1)]
>> x

[matrix([[ 0.77886042],
        [ 0.51142657],
        [ 0.68692362]]),
 matrix([[ 0.01367274],
        [ 0.24491876],
        [ 0.74441998]]),
 matrix([[ 0.35809997],
        [ 0.12779427],
        [ 0.3057233 ]])]
>> sum(x)

matrix([[ 1.15063313],
        [ 0.8841396 ],
        [ 1.7370669 ]])


More information about the Numpy-discussion mailing list