how to transfer a cell array content into ordinal values?

1 Ansicht (letzte 30 Tage)
chocho
chocho am 9 Apr. 2017
Bearbeitet: Stephen23 am 10 Apr. 2017
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.

Akzeptierte Antwort

chocho
chocho am 9 Apr. 2017
Bearbeitet: Stephen23 am 9 Apr. 2017
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 Kommentar
Stephen23
Stephen23 am 10 Apr. 2017
Bearbeitet: Stephen23 am 10 Apr. 2017
@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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 9 Apr. 2017
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 am 9 Apr. 2017
Bearbeitet: Stephen23 am 9 Apr. 2017
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by