How to group and convert to decimal?
Show older comments
The output of r is 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1
I want to group every 8 digits and convert to decimal
Please help
4 Comments
Please show what you have done so far.
doc bin2dec
doc reshape
Image Analyst
on 4 Feb 2013
Is the output of your r() function a string, or a numerical array (such as double or int32 or logical)? If it's a string, are there spaces between the digits?
Sharen H
on 4 Feb 2013
Image Analyst
on 4 Feb 2013
Edited: Image Analyst
on 4 Feb 2013
You mean an "array" of doubles is returned by r(), like this [0, 0, 0, 1, 0, 1, 0, etc.], not just a single double like you implied. So, it's like what Azzi assumed.
Accepted Answer
More Answers (1)
Converting from a double vector to a string and back to a double inside bin2dec is not efficient. Better convert from binary to decimal values directly:
x = [ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1];
d = 2 .^ (length(x)-1:-1:0) * x(:);
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!