converting specific string variables to double

5 Ansichten (letzte 30 Tage)
antonet
antonet am 15 Jun. 2013
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!

Akzeptierte Antwort

the cyclist
the cyclist am 15 Jun. 2013
I knew there was an easier way:
A(ismember(A,'0'))={0}

Weitere Antworten (3)

Jan
Jan am 17 Jun. 2013
A(strcmp(A, '0')) = {0}
  1 Kommentar
Azzi Abdelmalek
Azzi Abdelmalek am 17 Jun. 2013
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.

Melden Sie sich an, um zu kommentieren.


the cyclist
the cyclist am 15 Jun. 2013
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 am 15 Jun. 2013
Bearbeitet: Azzi Abdelmalek am 15 Jun. 2013
A(~cellfun('isempty',strfind(A,'0')))={0}

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by