Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Imagine I have something like this:

 import numpy as np
 arra = np.arange(16).reshape(2, 2, 4)  

which gives

 array([[[0, 1, 2, 3],  
      [4, 5, 6, 7]],  
      [[8, 9, 10, 11],  
      [12, 13, 14, 15]]])     

and I want to make a loop that runs along specific subarrays (in this case, I want to run along each 'column' of each 'matrix') and sum the result (that is, selecting 0 and 4 and summing them (4), selecting 1 and 5 and summing them (6), ..., selecting 3 and 7 and summing them (10), selecting 8 and 12 and summing them (20), ..., selecting 11 and 15 and summing them (26)).

I had thought doing that with the apparently natural:

 for i in arra[i, j, k]:  
     for j in arra[i, j, k]:  
         for k in arra[i, j, k]:  
             sum...  

The problem is that Python certainly doesn't allow to do what I want in this way. If it were a 2D array it would be easier as I know that the iterator first runs through the rows, so you can transpose to run along the columns, but for a multidimensional (3D in this case) array (N, M, P) with N, M, P >> 1, I was wondering how it could be done.

EDIT: This question has a continuation here: Choosing and iterating specific sub-arrays in multidimensional arrays in Python

share|improve this question
up vote 2 down vote accepted

You can use map to get this done:

import numpy as np
arra = np.arange(16).reshape(2, 2, 4)  

Then the command:

map(sum, arra)

gives you the desired output:

[array([ 4,  6,  8, 10]), array([20, 22, 24, 26])]

Alternatively, you can also use a list comprehension:

res = [sum(ai) for ai in arra]

Then res looks like this:

[array([ 4,  6,  8, 10]), array([20, 22, 24, 26])]

EDIT:

If you want to add identical rows - as you mentioned in the comments below this answer - you can do (using zip):

map(sum, zip(*arra))

which gives you the desired output:

[array([ 8, 10, 12, 14]), array([16, 18, 20, 22])]

For the sake of completeness also the list comprehension:

[sum(ai) for ai in zip(*arra)]

which gives you the same output.

share|improve this answer
    
Thanks, your answer is very useful, but I was wondering..., imagine you are interested, in the same example as before, in summing the values of the even rows, that is, 0 + 8, 1 + 9, 2 + 10, ..., 4 + 12, 7 + 15 (summing by columns but in even rows). Is there a general way to do it? – David Mar 3 at 13:01
1  
@David: Sure. I edited my answer and added the solution for that as well. – Cleb Mar 3 at 13:13
    
Thank you but I was still wondering if there are more generic ways. Imagine I want to add the first with the last, and the intermediate, that is, 0 + 12, 1 + 13, ..., 4 + 8, 5 + 9, ... 7 + 11. Is it still possible? Would you need a clever transposing of the axes? – David Mar 3 at 13:45
    
Then you might want to open a new question and use this question here as reference; it does not make that much sense to discuss all kind of special cases in the comments. – Cleb Mar 3 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.