Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Submatrix in Numpy without Loop

Tags:

python

numpy

Let's say I got this a = np.arange(9).reshape((3,3)) I want get a numpy array of [9,12,15] which is a result of

[0+3+6, 1+4+7, 2+5+8]
like image 701
Jasper Bernales Avatar asked Jan 07 '23 07:01

Jasper Bernales


1 Answers

You can usenumpy.array.sum() function by passing the axis=0:

>>> a.sum(axis=0)
array([ 9, 12, 15])
like image 54
Mazdak Avatar answered Jan 19 '23 01:01

Mazdak