title: Grouping elements of a numpy array using an array of group counts
body: <p>Given two arrays, one representing a stream of data, and another representing group counts, such as:</p>
  
  <pre><code>import numpy as np
  
  # given group counts:       3            4           3         2
  # given flattened data:[ 0, 1, 2,   3, 4, 5, 6,   7, 8, 9,   10, 11 ]
  
  group_counts = np.array([3,4,3,2])
  data = np.arange(group_counts.sum()) # placeholder data, real life application will be a very large array
  </code></pre>
  
  <p>I want to generate matrices based on the group counts for the streamed data, such as:</p>
  
  <pre><code>target_count = 3 # I want to make a matrix of all data items who's group_counts = target_count
  # Expected result
  # [[ 0  1  2]
  #  [ 7  8  9]]
  </code></pre>
  
  <p>To do this I wrote the following:</p>
  
  <pre><code># Find all matches
  match = np.where(groups == group_target)[0]
  i1 = np.cumsum(groups)[match] # start index for slicing
  i0 = i1 - groups[match] # end index for slicing
  
  # Prep the blank matrix and fill with resuls
  matched_matrix = np.empty((match.size,target_count))
  
  # Is it possible to get rid of this loop?
  for i in xrange(match.size):
      matched_matrix[i] = data[i0[i]:i1[i]]
  
  matched_matrix
  # Result: array([[ 0,  1,  2],
                   [ 7,  8,  9]]) # 
  </code></pre>
  
  <p>This works, but I would like to get rid of the loop and I can't figure out how.</p>
  
  <p>Doing some research I did find <code>numpy.split</code> and <code>numpy.array_split</code>:</p>
  
  <pre><code>match = np.where(group_counts == target_count)[0]
  match = np.array(np.split(data,np.cumsum(groups)))[match]
  # Result: array([array([0, 1, 2]), array([7, 8, 9])], dtype=object) # 
  </code></pre>
  
  <p>But <code>numpy.split</code> produces a list of <code>dtype=object</code> that I have to convert.</p>
  
  <p>Is there an elegant way to produce the desired result without a loop?</p>