필터 지우기
필터 지우기

Assign Numeric Values to List of Strings

조회 수: 2 (최근 30일)
Calum
Calum 2015년 4월 7일
댓글: per isakson 2015년 4월 7일
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
per isakson 2015년 4월 7일
Character array:
X=['a';'a';'c';'h';'x';'x'];
Cell array:
X={'a';'a';'c';'h';'x';'x'}

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2015년 4월 7일
[~,~,Y]=unique(X);

Thorsten
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 :-))

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by