title: efficient indexing of an array
body: <p>Suppose that I have </p>
  
  <pre><code>z[7]={0, 0, 2, 0, 1, 2, 1}
  </code></pre>
  
  <p>that means- first observation allocated in group 0, second obs group 0, third group 2 etc
  and I want to write an efficient code to get an array of 3X? such that in the first row I have all the observations allocated in the first group, second row all the obs allocated in the second group etc.. something like</p>
  
  <pre><code>0, 1, 3
  4, 6 
  2, 5
  </code></pre>
  
  <p>and this must be general, maybe I could have</p>
  
  <pre><code>z={0, 0, 2, 0, 1, 2, 1, 3, 4, 2, 0, 4, 5, 5, 6, 7, 0}
  </code></pre>
  
  <p>so the number of columns is unknown</p>
  
  <p>I did do my homework and the code is attached to this message, but there must be a better way to do it. I believe that with pointers but I really do not know how.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(){
    int z[7]={0, 0, 2, 0, 1, 2, 1}, nj[3], iz[3][7], ip[3], i, j;
  
  
  for(j=0; j&lt;3; j++){
      ip[j] = 0;
      nj[j] = 0;
    }
  
    for(i=0; i &lt;7; i++ ){
      nj[z[i]] = nj[z[i]] + 1;
      iz[z[i]][ip[z[i]]] = i;
      ip[z[i]] = ip[z[i]] + 1;    
    }  
  
    for(j=0; j&lt;3 ;j++){
      for(i=0; i &lt; nj[j]; i++){
        printf("%d\t", iz[j][i]);
      }
      printf("\n");    
    }
    return 0;  
  }
  </code></pre>