Replacing non-alphabetic characters with numbers?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrew
am 22 Jan. 2015
Beantwortet: Matz Johansson Bergström
am 22 Jan. 2015
Hello everyone,
I'm trying to solve a question and it's asking me to take the variable:
TS1='%@3Gb6'
and convert the non-alphabetic characters to zeros and the alphabetic characters with ones, saving the result as a new variable. Ideally I'm trying to get a new variable to saves as:
result=[0 0 0 1 1 0]
I would greatly appreciate the help on this issue.
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 22 Jan. 2015
Bearbeitet: Azzi Abdelmalek
am 22 Jan. 2015
out=zeros(size(TS1));
out(regexpi(TS1,'[A-Z]','start'))=1
%OR
str=['A':'Z' 'a':'z'];
out=ismember(TS1,str)
0 Kommentare
Weitere Antworten (1)
Matz Johansson Bergström
am 22 Jan. 2015
Yet another answer, just for the "cellfun" of it ;-)
tmp = cellfun(@(x) isstrprop(x, 'alpha'), {TS1}, 'UniformOutput', false);
out = cell2mat(tmp)
First, check the property of each element (cellfun does this) of the cell array {TS1}, if 'alpha' then 1 otherwise 0. Then we need to convert to a matrix, which is a vector in this case. If you wish to sum the ones in the vector you might want to convert it to "double", it is logical now.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!