-1

Let's say I have the array:

 import numpy as np
 a = np.array([[[1], [3], [5]], [[2], [8], [6]]]

How can I sum all first rows together, all second rows together and so so? So, I the result I want is something like this

 [3, 11, 11] or [[3], [11], [11]]

It seems to be so simple but I can't find solution which doesn't require loops...

jjankowiak
  • 3,010
  • 6
  • 28
  • 45

1 Answers1

0

I think what you're looking for is np.sum where you sum over the 0th axis.

import numpy as np
a = np.array([[[1], [3], [5]], [[2], [8], [6]]])
b = a.sum(0)
# b = array([[3],[11],[11]])

Though your initial addition was off 1 + 2 = 3, 3 + 8 = 11, and 5 + 6 = 11 leaves us with 3, 11, 11

Alternatively, as NAN pointed out

np.sum(a, axis=(0,2))
# array([ 3, 11, 11])
Skam
  • 7,298
  • 4
  • 22
  • 31