title: Vectorized programming with Matlab
body: <p>I have a vector J which contains the row index of a vector.
  <s>I would like to do the following code in a vectorized way :</s> I want to sum the duplicate values to the variable 'Mode' from the variable 'M'. I have <code>size(M)=size(J)</code>. This is my attempted code:</p>
  
  <pre><code>Mode=zeros(n,1);
  for i=1:length(J)
      Mode(J(i))=Mode(J(i))+M(i);
  end
  </code></pre>
  
  <p>I have tested with</p>
  
  <pre><code>Mode(J)=M
  </code></pre>
  
  <p>but the problem is that there are some duplicate index value in J.
  How can I implement it correctly ?</p>