MATLAB: I have a matrix column with a number in each row, but they are not consecutive (per example, from 3 it jumps to 6). How to turn them into a consecutive order

consecutivematrixorder

I have a matrix column with a number in each row, but they are not consecutive (per example, from 3 it jumps to 6). How can I turn them into a consecutive order?

Best Answer

A = [0,1,2,3,6,3,10,6,6,10,12,12,0,2];
[~, ~, idx] = unique(A);
t = 0:max(idx)-1;
B = t(idx);
Note: this code relies upon the expected lowest value being 0.