MATLAB Answers

0

vector of integers modification

Asked by Michal Kvasnicka on 18 Sep 2019
Latest activity Commented on by Michal Kvasnicka on 18 Sep 2019
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]

  3 Comments

madhan ravi
on 18 Sep 2019
Please don’t use variable as rank, MATLAB has inbuilt function named rank().
Guillaume
on 18 Sep 2019
There are so many functions in matlab that it can be difficult not to shadow one. If you're not doing matrix algebra it doesn't really matter if you stomp on rank.
On the other hand, rank is not a particularly good variable name. rank of what? It would be much better if that what was added to the variable name.
Of course, you are right! I just use rank variable name by my mistake... :)

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by Guillaume
on 18 Sep 2019
 Accepted Answer

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

  1 Comment

Simple and effective solution ... thanks

Sign in to comment.