partition of matrix element

how to split elements in a matrix into separate characters? for example: split [01001] into [0 1 0 0 1]

 Respuesta aceptada

Paulo Silva
Paulo Silva el 5 de Ag. de 2011

2 votos

b=dec2bin(100)
BinArray=b-'0'

2 comentarios

Fangjun Jiang
Fangjun Jiang el 5 de Ag. de 2011
Nice, Paulo! Saw this technique a few times now. Still not sticking in my mind.
Paulo Silva
Paulo Silva el 5 de Ag. de 2011
it's not easy to remember for me also :)

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 5 de Ag. de 2011

0 votos

for k=1:10
a=dec2bin(k,5);
b=str2num(a(:));
c=b'
end

4 comentarios

Talaria
Talaria el 5 de Ag. de 2011
great,
but this works if i input 01001 originally as a string, how to do the same if it is originally a binary number??
in other words if i have a binary number 01001, how do i convert it into a text string in order to apply str2num(a(:))??
Daniel Shub
Daniel Shub el 5 de Ag. de 2011
How are you creating your binary number? Can you give an example of what a='01001'; should be replaced by?
Talaria
Talaria el 5 de Ag. de 2011
the simple dec2bin,
a for loop creates a 1-column matrix,
for example:
[000 001 010 011 etc...]
Daniel Shub
Daniel Shub el 5 de Ag. de 2011
But dec2bin just returns a string: isequal('10110', dec2bin(22)). It seems to me the answer still works.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 5 de Ag. de 2011

0 votos

It appears to me that you expect to be able to enter a binary constant in the form of a number, such as entering 01001 and have that represent a 5 bit number. Unfortunately in MATLAB there is no syntax for entering binary numbers in numeric form. Entering 01001 in a numeric field is the same to MATLAB as entering 1001 meaning one thousand plus one.
You can enter numbers in decimal form such as 01001 and you can split the decimal value up as if it was binary, but in such a situation MATLAB will not be able to determine how many leading zeros you want. 0001001 is the same to MATLAB as 1001 .
The maximum number of bits that you can enter in decimal form and recover the exact binary of, is 16, 1111111111111111 .
If you know that a particular number X is represented as decimal is an N-bit binary number, then you can split it up by using
sprintf('%0*ld', N, X) - '0'
If your program is always working with the same number of bits (e.g., 9) you can hard-code that:
sprintf('%09ld', X) - '0'

2 comentarios

Fangjun Jiang
Fangjun Jiang el 5 de Ag. de 2011
Not exactly, Walter. I think you can do this:
s=input('Type in binary string and hit enter:','s');
BinArray=s-'0';
Walter Roberson
Walter Roberson el 5 de Ag. de 2011
That isn't entering binary numbers in numeric form: that is entering binary numbers in string form.
There are computer languages that allow octal constants or hexadecimal constants; some even support binary constants. For example in some languages, '01001'b would indicate a binary constant to the language.

Iniciar sesión para comentar.

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by