MATLAB: Vector of integers modification

MATLABorderingvector

I have vector of positive integers which I need to modify into vector of consecutive integers with the same ordering.
Example:
rank = [4 1 2 2 4] -> rank_new = [3 1 2 2 3]

Best Answer

Possibly:
rank = [4, 1, 2, 2, 4];
[~, ~, rank_new] = unique(rank)