Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

converting specific string variables to double

Asked by antonet on 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!

0 Comments

antonet

Tags

Products

No products are associated with this question.

4 Answers

Answer by the cyclist on 15 Jun 2013
Accepted answer

I knew there was an easier way:

A(ismember(A,'0'))={0}

1 Comment

Azzi Abdelmalek on 15 Jun 2013

This one is much faster

the cyclist
Answer by Jan Simon on 17 Jun 2013
Edited by Jan Simon on 17 Jun 2013
A(strcmp(A, '0')) = {0}

1 Comment

Azzi Abdelmalek on 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.
Jan Simon
Answer by the cyclist on 15 Jun 2013

I am quite sure there is a simpler way, but one way is

A(cellfun(@(x)isequal(x,'0'),A))={0};

0 Comments

the cyclist
Answer by Azzi Abdelmalek on 15 Jun 2013
Edited by Azzi Abdelmalek on 15 Jun 2013
A(~cellfun('isempty',strfind(A,'0')))={0}

0 Comments

Azzi Abdelmalek

Contact us