Assign Numeric Values to List of Strings
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
per isakson
2015-4-7
Character array:
X=['a';'a';'c';'h';'x';'x'];
Cell array:
X={'a';'a';'c';'h';'x';'x'}
回答(2 个)
Thorsten
2015-4-7
That's easy
Y = [ 1 1 + cumsum(diff(X - 'a' + 1) > 0)'];
(and one of the reasons why I love Matlab :-))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!