title: Numpy sum between pairs of indices in 2d array
body: <p>I have a 2-d numpy array (MxN) and two more 1-d arrays (Mx1) that represent starting and ending indices for each row of the 2-d array that I'd like to sum over. I'm looking for the most efficient way to do this in a large array (preferably without having to use a loop, which is what I'm currently doing). An example of what i'd like to do is the following.</p>
  
  <pre><code>&gt;&gt;&gt; random.seed(1234)
  &gt;&gt;&gt; a = random.rand(4,4)
  &gt;&gt;&gt; print a
  [[ 0.19151945  0.62210877  0.43772774  0.78535858]
   [ 0.77997581  0.27259261  0.27646426  0.80187218]
   [ 0.95813935  0.87593263  0.35781727  0.50099513]
   [ 0.68346294  0.71270203  0.37025075  0.56119619]]
  &gt;&gt;&gt; b = array([1,0,2,1])
  &gt;&gt;&gt; c = array([3,2,4,4])
  &gt;&gt;&gt; d = empty(4)
  &gt;&gt;&gt; for i in xrange(4):
      d[i] = sum(a[i, b[i]:c[i]]) 
  
  &gt;&gt;&gt; print d
  [ 1.05983651  1.05256841  0.8588124   1.64414897]
  </code></pre>
  
  <p>My problem is similar to the following question, however, I don't think the solution presented there would be very efficient. <a href="https://stackoverflow.com/questions/7471474/numpy-sum-of-values-in-subarrays-between-pairs-of-indices">Numpy sum of values in subarrays between pairs of indices</a> In that question, they are wanting to find the sum of multiple subsets for the same row, so <code>cumsum()</code> can be used. However, I will only be finding one sum per row, so I don't think this would be the most efficient means of computing the sum. </p>
  
  <p>Edit: I'm sorry, I made a mistake in my code. The line inside the loop previously read <code>d[i] = sum(a[b[i]:c[i]])</code>. I forgot the index for the first dimension. Each set of starting and ending indices corresponds to a new row in the 2-d array.</p>