Effacer les filtres
Effacer les filtres

creating dummies from a cell vector of string variables

3 vues (au cours des 30 derniers jours)
Sabbas
Sabbas le 27 Sep 2012
Dear all,
I have this cell vector
A={' ps1'
' ps1'
' ps1'
' ps1'
' ps2'
' ps2'
' ps2'
' ps2'
' ps3'
' ps3'
' ps3'
' ps3'
' ps4'
' ps4'
' ps4'}
I would like to construct dummies so as to have 1 for 'ps1', 2 for 'ps2' and so forth. Is there a way of doing that?. I tried str2double but it does not work for vectors as i get a vector of NaNs
thanks

Réponses (3)

Tom
Tom le 27 Sep 2012
B=str2num(cellfun(@(x) x(end),A))

Jan
Jan le 27 Sep 2012
Modifié(e) : Jan le 27 Sep 2012
str2double fails due to the leading part ' ps'. So you could remove it:
A={' ps1'; ' ps1'; ' ps1'; ' ps1'; ' ps2'; ...
' ps2'; ' ps2'; ' ps2'; ' ps3'; ' ps3'; ...
' ps3'; ' ps3'; ' ps4'; ' ps4'; ' ps4'};
B = strrep(A, ' ps', '');
Num = str2double(B);
But this is faster such that it reduces the CO2 production:
Str = sprintf('%s*', A{:});
Num = sscanf(Str, 'ps%d*');

Andrei Bobrov
Andrei Bobrov le 27 Sep 2012
[c c c] = unique(A)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by