필터 지우기
필터 지우기

How to convert logical into decimal number?

조회 수: 15 (최근 30일)
Anisa
Anisa 2013년 1월 13일
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
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일
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
Walter Roberson
Walter Roberson 2013년 1월 16일
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일
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);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by