converting specific string variables to double

조회 수: 1 (최근 30일)
antonet
antonet 2013년 6월 15일
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!

채택된 답변

the cyclist
the cyclist 2013년 6월 15일
I knew there was an easier way:
A(ismember(A,'0'))={0}

추가 답변 (3개)

Jan
Jan 2013년 6월 17일
A(strcmp(A, '0')) = {0}
  댓글 수: 1
Azzi Abdelmalek
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
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};

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 15일
편집: Azzi Abdelmalek 2013년 6월 15일
A(~cellfun('isempty',strfind(A,'0')))={0}

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by