title: MATLAB index within groups
body: <p>Given a vector <code>A</code> of group numbers (such as the one returned by <code>findgroups</code>), how to return a vector <code>B</code> of the same length containing indices of elements within groups of <code>A</code>?</p>
  
  <p>For example, if <code>A = [1 1 1 2 2 2 1 1 2 2]</code> then <code>B = [1 2 3 1 2 3 4 5 4 5]</code>.</p>
  
  <p><strong>Add 1</strong></p>
  
  <p>My own solution to this is</p>
  
  <pre><code>s = splitapply(@(x) {x, [1:numel(x)]'}, [1:numel(A)]', A(:))
  B(vertcat(s{:,1})) = vertcat(s{:,2})
  </code></pre>
  
  <p>but it seems somewhat convoluted.</p>