partition of matrix element

조회 수: 6(최근 30일)
Talaria 2011년 8월 5일
how to split elements in a matrix into separate characters? for example: split [01001] into [0 1 0 0 1]

채택된 답변

Paulo Silva
Paulo Silva 2011년 8월 5일
  댓글 수: 2
Paulo Silva
Paulo Silva 2011년 8월 5일
it's not easy to remember for me also :)

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

추가 답변(2개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 5일
for k=1:10
  댓글 수: 4
Daniel Shub
Daniel Shub 2011년 8월 5일
But dec2bin just returns a string: isequal('10110', dec2bin(22)). It seems to me the answer still works.

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

Walter Roberson
Walter Roberson 2011년 8월 5일
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
Walter Roberson
Walter Roberson 2011년 8월 5일
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.

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


Community Treasure Hunt

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

Start Hunting!

Translated by