title: MATLAB - Find and number duplicates within an array
body: <p>I have an array of values, some of which have duplicates, for example:</p>
  
  <pre><code>a = [5;5;4;7;7;3;3;9;5;7]
  </code></pre>
  
  <p>and I would like to find which are duplicates, and then number each of these sequentially, while making non-duplicates zero. For example:</p>
  
  <pre><code>b = [1;1;0;2;2;3;3;0;1;2]
  </code></pre>
  
  <p>Currently I have a very inefficient and incomplete approach, using the <code>unique</code> function and various <code>for</code> loops and <code>if</code> statements, but feel that there should be a simple answer.</p>
  
  <p>What is the most efficient way to get to this answer?</p>