フィルターのクリア

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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by