Is there a way to get an 8 x 1 matrix instead of 8 x 3 using dec2bin or any other?
8 views (last 30 days)
Show older comments
sawasawa
on 27 Feb 2021
Commented: Walter Roberson
on 27 Feb 2021
dec2bin (2 ^ n-1: -1: 0) - '0'
2 Comments
Image Analyst
on 27 Feb 2021
Edited: Image Analyst
on 27 Feb 2021
Give an example using some specific value of n and show what you got and what you want to get.
Accepted Answer
Walter Roberson
on 27 Feb 2021
string(dec2bin (2 ^ n-1: -1: 0))
This will be considered an array. The entries will be string objects such as "011". The entries will not be numeric.
1 Comment
Walter Roberson
on 27 Feb 2021
Perhaps you want
n=3
b = (dec2bin (2 ^ n-1: -1: 0) - '0')*10.^(n-1:-1:0).'
Note that if you do this, then the only way to get the leading zeros is to format it as text again.
More Answers (1)
Jan
on 27 Feb 2021
Edited: Jan
on 27 Feb 2021
If you want 000 as output, remember, that this is not a valid decimal value. If you need the zeros for any reason, you have to stay at the char representation. Simply omit the -'0' part:
n = 3;
b = dec2bin(2 ^ n-1:-1:0)
The cannot be a 8x1 matrix.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!