how to convert 1100111 to 1 1 0 0 1 1 1

조회 수: 2 (최근 30일)
imola
imola 2015년 6월 26일
편집: Stephen23 2015년 6월 26일
Dear All,
I need to convert a matrix of
a=[11100;10000;10101];
to
s=[1 1 1 0 0;1 0 0 0 0;1 0 1 0 1];
to convert s to a we use
char(s+'0');
but how form a to s. Thanks in advance for any help.
regards,
Imola

채택된 답변

Adam
Adam 2015년 6월 26일
s = arrayfun( @(x) str2double( x ), num2str( a ) )
  댓글 수: 1
Stephen23
Stephen23 2015년 6월 26일
편집: Stephen23 2015년 6월 26일
There is no need to use such complicated and slow code to solve this. For a much faster and simpler solution see my answer below.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephen23
Stephen23 2015년 6월 26일
편집: Stephen23 2015년 6월 26일
If a is a numeric column vector, then you can simply use num2str like this:
>> a = [11100;10000;10101]
>> s = num2str(a)-'0'
s =
1 1 1 0 0
1 0 0 0 0
1 0 1 0 1
If a is actually a character array, then simply do this:
>> a = ['11100';'10000';'10101']
>> s = a-'0'
s =
1 1 1 0 0
1 0 0 0 0
1 0 1 0 1

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 26일
a={'11100';'10000';'10101'}
out=cell2mat(cellfun(@(x) x-'0',a,'un',0))

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by