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

4 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2014-9-29
编辑:Stephen23 2014-9-29
Try this:
'1010' - '0'
or equivalently:
'1010' - 48

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-9-29
str2double(regexp('1010','\d','match'))

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by