Effacer les filtres
Effacer les filtres

Assign Numeric Values to List of Strings

2 vues (au cours des 30 derniers jours)
Calum
Calum le 7 Avr 2015
Commenté : per isakson le 7 Avr 2015
Hi,
If I have a cell array vertically sorted alphabetically as follows:
X=['a';'a';'c';'h';'x';'x']
What code should I use to obtain the numeric output:
Y=[1;1;2;3;4;4]
Where each integer in Y represents the alphabetic rank of the corresponding string in X.
Many thanks,
Cal
  1 commentaire
per isakson
per isakson le 7 Avr 2015
Character array:
X=['a';'a';'c';'h';'x';'x'];
Cell array:
X={'a';'a';'c';'h';'x';'x'}

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 7 Avr 2015
[~,~,Y]=unique(X);

Thorsten
Thorsten le 7 Avr 2015
That's easy
Y = [ 1 1 + cumsum(diff(X - 'a' + 1) > 0)'];
(and one of the reasons why I love Matlab :-))

Catégories

En savoir plus sur Characters and Strings 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!

Translated by