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

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by