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

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
Anisa 2013년 1월 14일
the longest is 103.
Jan
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)?
I have matrix C size 1x1013 cell. Each cell consists of binary bit (logical) with different size (the longest is 103 bits). I want to convert each cell on matrix C{i} into decimal number. For example:
C{i} = {[1 0 0 0 1 1 0 1 0 1], [0 1 1], [1 0 0 1 1], [1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 1 1], [1 0 1 0 1 0 1 0]}
I want the results on decimal. Like this:
C{} = {[565],[3],[19],[10252451],[170]}
I have tried used bin2dec but it works only 52 bits or less, since my longest bit is 103. So i try this manual converting.
z = [1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 1];
z=logical(z);
x=length(z);
for i=1:x;
f(i)=(z(i)*(2^(x-i)));
end
dec=sum(f)
And here is the results:
dec = 2.9171e+010
What does the 'e+010' mean? How to apply the function into each cell? If use 'cellfun(fun, A)', how to do it? Since i have no idea how to set up the 'fun' correctly.
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
Anisa 2013년 1월 26일
I'm so sorry Jan. I didn't mean to ignore them. I'm a newbie here and still learn how to use matlab. I'm sorry.
And thank you for the solution. I already try it. Thank you.
Carlos Lara
Carlos Lara 2013년 7월 2일
I think .. It could be solved by using the 'bitset' function

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2013년 1월 13일

1 개 추천

Assuming that the highest significant bit comes on the left:
C = {logical(randi(2,1,10) - 1), logical(randi(2,1,40) - 1)};
P2 = transpose(2 .^ (52:-1:0));
D = zeros(size(C));
for iD = 1:numel(D)
aC = C{iD};
D(iD) = aC * P2(54 - length(aC):53);
end
This is much more efficient than converting the logical vector to a string, the string to a double vector (inside bin2dec) and wrap this by cellfun and a slow anoymous function. I am really a fan of cellfun, but only, if it is efficient.

댓글 수: 9

Andrei Bobrov
Andrei Bobrov 2013년 1월 13일
+1
Anisa
Anisa 2013년 1월 14일
how do I do this since the longest logical value is 103? is there another way?
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 14일
편집: Azzi Abdelmalek 2013년 1월 25일
Uing Jan's idea
C = {logical(randi(2,1,103) - 1), logical(randi(2,1,100) - 1)};
idx=2.^(0:102)';
out=zeros(size(C))
for k=1:numel(C)
out{k}=C{k}*flipud(idx(1:numel(C{k})))
end
Jan
Jan 2013년 1월 14일
@Azzi: My first name is Jan. It is fine to use it in the forum. Instead of flipping the data, it would be more efficient to flip the vector of the powers of two once only. Using the dot product is faster than performing sum(a.*b). If the data vae 103 bits, it is enough to get the powers of two from 0 to 102, instead of 103.
@Anisa: You cannot store a value until 2^102 in a DOUBLE without loosing information. This works until 2^52 only. If this does not matter, you can consider the first 53 bits only. So please explain, what you want to achieve exactly.
José-Luis
José-Luis 2013년 1월 14일
편집: José-Luis 2013년 1월 14일
Well, not to be pedantic or anything :), but you can store values larger than 2^53 - 1 without losing information, provided that they don't have more than 53 bits (15 digits) of significant values. This doesn't help Anisa, though.
Jan
Jan 2013년 1월 14일
@Jose-Luis: Correct. The binary vector [ones(1,52), zeros(1,102)] can be represented exactly (or perhaps ones(1,53)?).
Anisa
Anisa 2013년 1월 16일
Actually i want to convert it into different base-N number.
Are you looking for strings as output, or numeric values? If you are looking for numeric values as output, you are not going to be able to produce that for more than 53 bits, fewer if you want to encode your numeric base in decimal (e.g., 333 decimal to mean 4^3 - 1 in base 4).
Longer numbers can be constructed as symbolic numbers if you have the symbolic toolbox, or there is John D'Errico's vpi (Variable Precision Integer) contribution.
Jan
Jan 2013년 1월 16일
@Anisa: It would be helpful, if you answer the questions in the comments. Please explain, what you exactly want as output and do not let us guess the details. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

José-Luis
José-Luis 2013년 1월 13일

0 개 추천

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에 대해 자세히 알아보기

태그

질문:

2013년 1월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by