MATLAB: Collapsing integer data into consecutive integers

cell arrays

I have a matrix of values that include integers >= 0. There are repeats, and also gaps in consecutiveness. I would like to collapse the data into consecutive integers, keeping the order intact.
Example: [ 0 1 4 4 1 5 8 1 4]
would go to
[ 0 1 2 2 1 3 4 1 2]
I can do this with loops and brute force, but I'm wondering if there's any quick way to do this since it is frequently called within a loop?
Thanks!

Best Answer

[~,~,I] = unique([ 0 1 4 4 1 5 8 1 4]);
disp(I-1)
Related Question