Way to set a cell array of strings to 0 or 1 depending on the string?
2 次查看(过去 30 天)
显示 更早的评论
I have a cell array of strings (1000x1 cell) containing either "On" or "Off". Is there a simple command to replace "On" by 1 and "Off" by 0 in the whole array? I would like to avoid using a FOR loop. Thanks!
0 个评论
采纳的回答
the cyclist
2015-11-11
编辑:the cyclist
2015-11-11
One way:
c = {'On','Off','On'};
tf = strcmp(c,{'On'})
0 个评论
更多回答(1 个)
the cyclist
2015-11-11
One way:
c = {'On','Off','On'}
tf = ismember(c,{'On'})
Then do
x = double(tf)
if you really need numeric, rather than the logical array tf.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!