title: Find number of consecutive elements before value changes (MATLAB)
body: <p>I have a (row)vector of some size, containing the values 1,2 and 3. They are in there in no 'specific' order, so a sample of the array would be [1,1,1,1,2,2,2,1,1,2,2,3,3]. What I want to do is find the consecutive number of identical elements, but with some restrictions.</p>
  
  <p>What I want is to make new arrays of which the elements denote:</p>
  
  <ul>
  <li>The number of consecutive 1's before it changes into a 2 </li>
  <li>The number of consecutive 2's before it changes into a 1</li>
  <li>The number of consecutive 2's before it changes into a 3</li>
  <li>The number of consecutive 3's before it changes into a 2</li>
  </ul>
  
  <p>So for the example I have given, the arrays would be</p>
  
  <pre><code>[4,2]
  [3]
  [2]
  []
  </code></pre>
  
  <p>I'm not sure how to tackle this. I can use the diff function to find where it changes sign, but then it'll be a little tough to figure out exactly what change has occured, right?</p>
  
  <p>The method does not have to be super fast, as I only have to do this a few times for around 10^5 datapoints.</p>