How to map a set of integer IDs to serial natural numbers?

Hello,
I'm trying to map a set of IDs which are integers to a list of serial numbers. So, for example, the IDs are as follows:
[12 13 20 12 5 12 13]
then the output should be
[2 3 4 2 1 2 3]
Thus, 5 has been mapped to 1 and 20 has been mapped to 4.
Is there a clever and efficient way to accomplish this? I believe the command 'unique' should help, but I'm not able to find a way to use it.

 Accepted Answer

[u,a,b] = unique([12 13 20 12 5 12 13]);
and then b is the vector you want.

1 Comment

Now I feel like an idiot for not reading the documentation on unique carefully.
Thanks, that was fast!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!