vector of integers modification

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 comentarios

madhan ravi
madhan ravi el 18 de Sept. de 2019
Please don’t use variable as rank, MATLAB has inbuilt function named rank().
Guillaume
Guillaume el 18 de Sept. de 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.
Michal
Michal el 18 de Sept. de 2019
Of course, you are right! I just use rank variable name by my mistake... :)

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 18 de Sept. de 2019
Possibly:
rank = [4, 1, 2, 2, 4];
[~, ~, rank_new] = unique(rank)

1 comentario

Michal
Michal el 18 de Sept. de 2019
Simple and effective solution ... thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 18 de Sept. de 2019

Comentada:

el 18 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by