Ryan
Ryan

Reputation: 10109

Concatenating or adding elements of an array

i have a numpy array of 27 elements,Im trying concatenate or add all the elements inside the array,but i cant come up with anything right,

I tried,

for index,value in enumerate(array):
    np.concatenate(array[index],array[index])

but this throws

TypeError: only integer scalar arrays can be converted to a scalar index

I tried

array[1]+array[2]+array[3]

this works for me, but im not sure how to put this in a loop, Any suggestions on this front would be really helpful

Thanks in advance.

EDIT: array looks like this

array([[[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0]],

   ..., 
   [[0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    [0, 0, 0, ..., 0, 0, 0],
    ..., 

Upvotes: 1

Views: 99

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

You want to sum over one axis (the first, I think). Like this:

array.sum(axis=0)

Upvotes: 2

Related Questions