Find all unique values and replace it with new values
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Elysi Cochin
le 6 Juin 2022
Commenté : Image Analyst
le 7 Juin 2022
I have a vector with values
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
I would like to find the unique values and replace the vector with numbers from 1, like
newV = [1; 2; 3; 1 ; 3; 4; 4; 5];
0 commentaires
Réponse acceptée
Walter Roberson
le 6 Juin 2022
findgroups()
But since you are using floating point numbers you really should use the third output of uniquetol() instead
4 commentaires
Image Analyst
le 7 Juin 2022
@Elysi Cochin I'm pretty sure you've seen the FAQ on this before but here it is again:
Plus de réponses (2)
KSSV
le 6 Juin 2022
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
[c,ia,newV] = unique(v,'stable') ;
newV
0 commentaires
Image Analyst
le 6 Juin 2022
Will this do:
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
% newV = [1; 2; 3; 1 ; 3; 4; 4; 5];
[g, Tid] = findgroups(v)
I know it doesn't match your desired but it's easy and maybe you don't need that exact order. If you need that exact order, can you explain why you need that order? (And don't jsut say I need it because I need it.)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!