function y = countreps( x )
%y = countreps( x )
%   X is a vector.  X is divided into runs of repeated elements.
%   Y is set to an N*2 array in which row i = [a,b] represents the i'th run
%   by the value A  and the number of repetitions B.

    y = zeros(0,2);
    ny = 0;
    if isempty(x)
        return;
    end
    a = x(1);
    b = 1;
    for i=2:length(x)
        a1 = x(i);
        if a==a1
            b = b+1;
        else
            ny = ny+1;
            y(ny,:) = [a,b];
            a = a1;
            b = 1;
        end
    end
    ny = ny+1;
    y(ny,:) = [a,b];
end
