I offer you my function runindex which does exactly this, fast and vectorised!
A = [1 1 1 5 5 8 9 9 9 9 10 11]
B = runindex(A)
%-> [1 2 3 1 2 1 1 2 3 4 1 1]
runindex can be downloaded for free from the Matlab File Exchange:
... and if you find it useful, please give some feedback :)
