Filter löschen
Filter löschen

Convert a cell of 'yes' and 'no' into array of 1 and 0

3 Ansichten (letzte 30 Tage)
Mike Mierlo van
Mike Mierlo van am 23 Apr. 2020
Beantwortet: Walter Roberson am 23 Apr. 2020
Hi guys,
I have a problem converting a cell of 'yes' and 'no' (string) into array of 1 and 0 (double).
Lets say I have the following cell:
Data = 4×1 cell array
{'no' }
{'yes'}
{'no' }
{'yes'}
The output should be:
Data = [0;1;0;1]
I have the following code that works when the cell only contains 'yes' but does not when it contains a 'no'.
for i=1:length(Data)
if Data{i}=='yes'
Data{i}=1;
else
Data{i}=0;
end
end
Data=cell2mat(Data);

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Apr. 2020
out = strcmpi(Data,'yes')

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 23 Apr. 2020
ismember(Data, 'yes')
no need to test for 'no' if you know it will always be one or the other.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by