converting specific string variables to double
3 次查看(过去 30 天)
显示 更早的评论
Dear all,
I have the following matrix
A={'name'
'afsaf'
'sfsfs'
'0'
'rpytui'
'0'
'0'
'0'
'dfgl'
'trd'
};
I want to convert the zeros which are string variables to numeric variables; that is
A={'name'
'afsaf'
'sfsfs'
[0]
'rpytui'
[0]
[0]
[0]
'dfgl'
'trd'
};
Is there a way of doing that?
Thanks in advance!
0 个评论
采纳的回答
更多回答(3 个)
Jan
2013-6-17
A(strcmp(A, '0')) = {0}
1 个评论
Azzi Abdelmalek
2013-6-17
This is faster
A=repmat(A,100000,1);
tic
A(ismember(A,'0'))={0};
toc
tic
A(strcmp(A, '0')) = {0};
toc
Elapsed time is 0.047910 seconds.
Elapsed time is 0.012593 seconds.
the cyclist
2013-6-15
I am quite sure there is a simpler way, but one way is
A(cellfun(@(x)isequal(x,'0'),A))={0};
0 个评论
Azzi Abdelmalek
2013-6-15
编辑:Azzi Abdelmalek
2013-6-15
A(~cellfun('isempty',strfind(A,'0')))={0}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!