Assign Numeric Values to List of Strings
조회 수: 2 (최근 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!