How to convert logical into decimal number?
Afficher commentaires plus anciens
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 commentaires
Jan
le 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
le 14 Jan 2013
Jan
le 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
le 25 Jan 2013
Jan
le 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
le 26 Jan 2013
Carlos Lara
le 2 Juil 2013
I think .. It could be solved by using the 'bitset' function
Réponse acceptée
Plus de réponses (1)
José-Luis
le 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);
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!