-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...

1
  • Last one - 11? a.sum(0)? Commented Nov 17, 2017 at 21:20

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

or np.sum(a, axis=(0,2)) if you just want it like array([ 3, 11, 11])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.