title: Updating a matrix from list of redundant indices
body: <p>I have a matrix <code>A</code>, a list of indices <code>is</code> and <code>js</code>, and a list of values to add to <code>A</code>, <code>ws</code>. Originally I was simply iterating through <code>A</code> by a nested for loop:</p>
  <pre><code>for idx = 1:N
      i = is(idx);
      j = js(idx);
      w = ws(idx);
      A(i,j) = A(i,j) + w;
  end
  </code></pre>
  <p>However, I would like to vectorize this to increase efficiency. I thought something simple like</p>
  <pre><code>A(is,js) = A(is,js) + ws
  </code></pre>
  <p>would work, and it does <em>as long as the is and js don't repeat</em>. Said differently, if I generate <code>idx = sub2ind(size(A),is,js);</code>, so long as <code>idx</code> has no repeat values, all is well. If it does, then only the last value is added, all previous values are left out. A concrete example:</p>
  <pre><code>A = zeros(3,3);
  indices = [1,2,3,1];
  additions = [5,5,5,5];
  A(indices) = A(indices) + additions;
  </code></pre>
  <p>This results in the first column having values of 5, not 5,5,10.</p>
  <p>This is a small example, but in my actual application the lists of indices are really long and filled with redundant values. I'm hoping to vectorize this to save time, so going through and eliminating redundancies isn't really an option. So my main question is, how do I add to a matrix from a given set of redundant indices? Alternatively, is there another way of working through this without any sort of iteration?</p>