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

2 Ansichten (letzte 30 Tage)
imola
imola am 26 Jun. 2015
Bearbeitet: Stephen23 am 26 Jun. 2015
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

Akzeptierte Antwort

Adam
Adam am 26 Jun. 2015
s = arrayfun( @(x) str2double( x ), num2str( a ) )
  1 Kommentar
Stephen23
Stephen23 am 26 Jun. 2015
Bearbeitet: Stephen23 am 26 Jun. 2015
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Stephen23
Stephen23 am 26 Jun. 2015
Bearbeitet: Stephen23 am 26 Jun. 2015
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 am 26 Jun. 2015
a={'11100';'10000';'10101'}
out=cell2mat(cellfun(@(x) x-'0',a,'un',0))

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by