title: efficiently update matrix element with a matrix of indices
body: <p>I have a matrix of indices I where some of the indices are repeated. I put an example below.</p>
  
  <p>I have another matrix A with dimensions compatible with the indices and initiated to 0 everywhere. I would like to do something like</p>
  
  <pre><code>A[I] += 1
  </code></pre>
  
  <p>I face two issues:</p>
  
  <ol>
  <li><code>A[I] = A[I] + 1</code> is too inefficient</li>
  <li>matrix <code>I</code> has redundant indices. For example rows 2 &amp; 6 are identical and I would like to obtain <code>A[1,2] = 2</code></li>
  </ol>
  
  <p>A partial answer would be to create a 3 columns matrix with the two first columns being the product of <code>unique(I)</code> and the third column with the counts, but I don't see any solution for that either. Any pointer or help would be greatly appreciated!</p>
  
  <pre><code>&gt; I is:
       [,1] [,2]
  [1,]    1    1
  [2,]    1    2
  [3,]    1    3
  [4,]    1    4
  [5,]    1    1
  [6,]    1    2
  [7,]    1    3
  </code></pre>