title: Numpy and Matlab difference in sum?
body: <p>I have a code that I am trying to translate from Matlab to Python however there is a problem with summation:</p>
  
  <pre><code>a=np.arange(1,28).reshape(3,3,3)
  print a
  print np.sum(np.sum(a,axis=1),axis=2)
  </code></pre>
  
  <p>gives me <code>axis index out of bound error</code>. According to the answer below I am updating this example.  The result for:</p>
  
  <pre><code>a=np.arange(1,28).reshape(3,3,3)
  print a
  print np.sum(np.sum(a,axis=1),axis=2)
  </code></pre>
  
  <p>is:</p>
  
  <pre><code>[[[ 1  2  3]
    [ 4  5  6]
    [ 7  8  9]]
  
   [[10 11 12]
    [13 14 15]
    [16 17 18]]
  
   [[19 20 21]
    [22 23 24]
    [25 26 27]]]
  [ 45 126 207]
  </code></pre>
  
  <p>but the same code in Matlab works fine:</p>
  
  <pre><code>a=1:27
  b=reshape(a,[3,3,3])
  b(:,:,1)=b(:,:,1)';
  b(:,:,2)=b(:,:,2)';
  b(:,:,3)=b(:,:,3)';
  b
  sum(sum(b,2),3)
  </code></pre>
  
  <p>Will give the following output:
  What is the problem?</p>
  
  <pre><code>b(:,:,1) =
  
       1     2     3
       4     5     6
       7     8     9
  
  
  b(:,:,2) =
  
      10    11    12
      13    14    15
      16    17    18
  
  
  b(:,:,3) =
  
      19    20    21
      22    23    24
      25    26    27
  
  ans =
  
          99
         126
         153
  </code></pre>