function v2 = removeConsecutiveRepetitions(v)
%REMOVECONSECUTIVEREPETITIONS Remove identical consecutive elements from a vector.
%   V = REMOVECONSECUTIVEREPETITIONS(V) remove the consecutive identical
%   elements of the vector V.
%
%   For example: REMOVECONSECUTIVEREPETITIONS([1 2 2 3 1]) = [1 2 3 1]

%   Author: Damien Teney

assert(numel(v) == length(v)); % Make sure we have a 1xN vector
assert(numel(v) > 0); % Make sure we have non-empty vector

v2 = zeros(1, length(v)); % Initialize (hopefully too big)
i2 = 0; % Number of values already in v2
for i = 1:length(v)
  if i2 == 0 || v(i) ~= v2(i2) % Different value than the previous one
    % Add it to v2
    i2 = i2 + 1;
    v2(i2) = v(i);
  end
end

% Keep only the actual values we put in v2
v2 = v2(1:i2);
