[Tutor] How to sum rows and columns of a matrix?
Pierre Barbier de Reuille
pierre.barbier at cirad.fr
Wed Feb 2 14:09:04 CET 2005
Even better :
>>> import numarray
>>> m = numarray.arange(16,shape=(4,4))
>>> numarray.sum(m)
array([24, 28, 32, 36])
>>> numarray.sum(axis=1)
array([ 6, 22, 38, 54])
Pierre
Kent Johnson a écrit :
> Kent Johnson wrote:
>
>> Liam Clarke wrote:
>>
>>> There's a specific package for arrays
>>> http://www.stsci.edu/resources/software_hardware/numarray
>>>
>>> that implements array mathematics. I use it for pixel map manipulation
>>> in pygame, so it's relatively fast.
>>
>>
>>
>> Here is one way to do what you want using numarray:
>
>
> Here is another way, probably more idiomatic and faster. (I'm just
> doodling with numarray so there may still be a better way to do this...)
>
> >>> import numarray
> >>> m=numarray.array(range(16),shape=(4,4))
>
> >>> numarray.add.reduce(m)
> array([24, 28, 32, 36])
>
> >>> numarray.add.reduce(m, axis=1)
> array([ 6, 22, 38, 54])
>
> Kent
>
>>
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
More information about the Tutor
mailing list