How to convert logical into decimal number?
古いコメントを表示
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 件のコメント
Jan
2013 年 1 月 13 日
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
2013 年 1 月 14 日
Jan
2013 年 1 月 14 日
@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
2013 年 1 月 25 日
Jan
2013 年 1 月 25 日
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
2013 年 1 月 26 日
Carlos Lara
2013 年 7 月 2 日
I think .. It could be solved by using the 'bitset' function
採用された回答
その他の回答 (1 件)
José-Luis
2013 年 1 月 13 日
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);
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!