How to convert logical into decimal number?
Show older comments
I have matrix C size 1x1013 cell. Each cell has different size.
<1x16 logical> <1x53 logical> <1x41 logical> <1x37 logical> <1x50 logical> <1x48 logical> and so on.
I want to convert each cell on matrix C into decimal number. I have tried use bin2dec but it doesn't worked.
For anyhelp, thank you.
7 Comments
Jan
on 13 Jan 2013
What is the longest logical vector in your cell? Logical vectors with more than 53 elements cannot be represented as DOUBLE number exactly:
2^53 - (2^53 + 1) % replies 0 !
Anisa
on 14 Jan 2013
Jan
on 14 Jan 2013
@Anisa: Whenever you write "it doesn't work" in the forum, be sure to add the reuiqred details: Do the results differ from your expectations (if so, how), or do you get an error message (if so, post a complete copy)?
Anisa
on 25 Jan 2013
Jan
on 25 Jan 2013
The notation "2.9171e+010" means 2.9171 * 10^10.
As explained already, the result is not exact. It is not only DEC2BIN whcih stops working at 52 bits, even the precision of numbers store in double variables is limited to this range. If the number has more bits, only the 52 most significant bits are considered. Try this:
2^54 + 1 == 2^54
I have posted a solution already, which works until 52 bits and handles a cell string also. It can be easily expanded to more bits, but again with the same limitation in the accuracy or the output.
There are still open questions for clarifications. When you want help, it would be a good idea not to ignore them, because they are essential for a solution. Imagine the effects, when you do not care about our questions.
Anisa
on 26 Jan 2013
Carlos Lara
on 2 Jul 2013
I think .. It could be solved by using the 'bitset' function
Accepted Answer
More Answers (1)
José-Luis
on 13 Jan 2013
a = randi(2,1,10)-1;
a = logical(a);
a = [{a};{a}]; %some cell array with logical values inside
%Turning into a string and then into a decimal number
your_vals = cellfun(@(x) bin2dec(sprintf('%i',x)),a);
Categories
Find more on Logical 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!