フィルターのクリア

how to transfer a cell array content into ordinal values?

1 回表示 (過去 30 日間)
chocho
chocho 2017 年 4 月 9 日
編集済み: Stephen23 2017 年 4 月 10 日
i have a cell array of string content and i want to turn the contents of cell 2 into ordinal values according to the list shown below in 'clinicalVAL.png'
catnames = { 'stage i'; 'stage ia'; 'stage ii'; 'stage iia'; 'stage iib'; 'stage iic'; 'stage iii'; 'stage iiia';'stage iiib'; 'stage iiic'; 'stage iv'; 'stage iva'; 'stage ivb'};
valueset={1;2;3;4};
for i=1:217
B = categorical(Ystg{i}(2),catnames,valueset,'Ordinal',true);
end
I have tried this code but doesn't work and i want to do it programmatically and not manually; is it possible in MATLAB?
ERROR: Error using categorical
Creating an instance of the Abstract class 'categorical' is not allowed.

採用された回答

chocho
chocho 2017 年 4 月 9 日
編集済み: Stephen23 2017 年 4 月 9 日
Hi Friends, I created this code and run it and is worked well, but i wonder if there is another more fast code that can do this job????
for i=1:217
if strcmpi(Ystg{i}(2),'stage i') || strcmpi(Ystg{i}(2),'stage ia')
Stage(i,1) = 1;
else if strcmpi(Ystg{i}(2),'stage ii') || strcmpi(Ystg{i}(2),'stage iia')|| strcmpi(Ystg{i}(2),'stage iib')|| strcmpi(Ystg{i}(2),'stage iic')
Stage(i,1) = 2;
else if strcmpi(Ystg{i}(2),'stage iii') || strcmpi(Ystg{i}(2),'stage iiia')|| strcmpi(Ystg{i}(2),'stage iiib')|| strcmpi(Ystg{i}(2),'stage iiic')
Stage(i,1) = 3;
else if strcmpi(Ystg{i}(2),'stage iv') || strcmpi(Ystg{i}(2),'stage iva')|| strcmpi(Ystg{i}(2),'stage ivb')
Stage(i,1) = 4;
end
end
end
end
end
  1 件のコメント
Stephen23
Stephen23 2017 年 4 月 10 日
編集済み: Stephen23 2017 年 4 月 10 日
@chocho: you accepted your own answer... even though you ask " i wonder if there is another more fast code that can do this job????"
You know that accepting your own answer discourages other people from helping you, because it tells everyone that this problem has been solved. It also prevents other people from gaining reputation points, when their answer is accepted.

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2017 年 4 月 9 日
Or simpler:
for i=1:217
switch Ystg{i}(2)
case {'stage i', 'stage ia'}
Stage(i,1) = 1;
case {'stage ii', 'stage iia', 'stage iib', 'stage iic'}
Stage(i,1) = 2;
case {'stage iii', 'stage iiia', 'stage iiib', 'stage iiic'}
Stage(i,1) = 3;
case {'stage iv', 'stage iva', 'stage ivb'}
Stage(i,1) = 4;
otehrwise % No switch without otherwise!
error('Unexpected value!');
end
  1 件のコメント
chocho
chocho 2017 年 4 月 10 日
@Jan Simon Thanks a lot

サインインしてコメントする。


Stephen23
Stephen23 2017 年 4 月 9 日
編集済み: Stephen23 2017 年 4 月 9 日
This is easy with regexp in just four lines, no loops or ifs or switches are required:
>> C = vertcat(Ystg{:});
>> D = strcat('\s(',{'i','ii','iii','iv'},')[a-d]?$');
>> E = cellfun(@(s)regexp(C(:,2),s,'once'),D,'Uni',0);
>> [F,~] = find(~cellfun('isempty',horzcat(E{:})).')
F =
3
3
2
2
2
2
3
2
1
2
1
3
2
1
3
3
3
4
4
2
2
3
3
...etc
  1 件のコメント
chocho
chocho 2017 年 4 月 10 日
@Stephen Cobeldick thank you too much

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by