MATLAB Answers

How to get [1 0 1 0] from '1010'

3 views (last 30 days)
Is there any built-in function or easiest way to get [1 0 1 0] from '1010' (character array)?
I have used for loop, but is there any other way to get the results?
str = '1010';
vec = zeros(length(str),1);
for i=1:length(str)
vec(i) = str2double(str(i));
end

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 29 Sep 2014
Edited: Stephen Cobeldick on 29 Sep 2014
Try this:
'1010' - '0'
or equivalently:
'1010' - 48

  1 Comment

Durga Lal Shrestha
Durga Lal Shrestha on 30 Sep 2014
Thank you. This is that easy.

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 29 Sep 2014
str2double(regexp('1010','\d','match'))

Tags