I need help in the the code with alternative to bin2dec function which gives output in the double class
I have been trying but no success. Can anyone help

댓글 수: 6

This sounds like a homework question. Then please post, what you have tried so far and ask a specific question.
Hint: To get the first bit, use:
x = 17;
rem(x, 2)
To get the others, you can divide by 2 successively and round.
Haseeb Hashim
Haseeb Hashim 2022년 3월 7일
I have tried similar to this but we have to divide the resulting quotient form first division. You have given the function which only gives us the remiander. ineed to automate the code.
Haseeb Hashim
Haseeb Hashim 2022년 3월 7일
편집: Haseeb Hashim 2022년 3월 7일
Oh I get it now, thanks for help man
I think we should use the floor function instead of round to get 3 if answer is 3.5
This is the floor() function. Another hint: You do not need loops:
x = 17;
y = x ./ [1, 2, 4, 8, 16]
With floor() you get the natural numbers and rem() extracts the first bit. You can express [1,2,4,8] as pow2(0:3).
Haseeb Hashim
Haseeb Hashim 2022년 3월 7일
편집: Rik 2022년 3월 7일
clc
clear
close all
num = 29;
i = 1;
flag = true;
while flag == true
bin(i) = rem(num,2);
if bin(i) == 0
num = num/2;
if num == 1
bin(i+1) = 1;
break
end
elseif bin(i) ~= 0
num = floor(num/2);
if num == 1
bin(i+1) = 1;
break
end
end
i = i + 1;
end
flip(bin)
I have made this logic and it gives the exactly right answers I was wondering that is there another efficient method which gives me what want but with efficient and easy code
Rik
Rik 2022년 3월 7일
If you want to improve this code: try to determine the number of bits you need. That way you can use array operations. That removes the need for a loop and prevents dynamically growing your bin array.

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

 채택된 답변

Jan
Jan 2022년 3월 7일
편집: Jan 2022년 3월 7일

0 개 추천

Some simplifications:
% Replace:
flag = true;
while flag == true
% by
while true
If you have checked for:
if bin(i) == 0
an
else
is enough and there is no reason to check for
elseif bin(i) ~= 0
again. But in your case there is no need to distinguish the 2 cases, because num = floor(num/2) works in all cases. Instead of catching the last bin manually, you can do this in the loop also:
num = 29;
i = 1;
while true
bin(i) = rem(num, 2);
num = floor(num / 2);
if num == 0
break
end
i = i + 1;
end
flip(bin)
Or even simpler: Check the condition instead of breaking an infinite loop:
num = 29;
bin = [];
while num > 0
bin(end+1) = rem(num, 2);
num = floor(num / 2);
end
flip(bin)
The Matlabish way is to omit the loop and to determine the number of bits in advance:
nBit = floor(log2(num)) + 1;
bin = rem(floor(num .* pow2(1-nBit:0)), 2);
Take the time to understand, what's going on here. Run it in pieces:
1-nBit:0
pow2(1-nBit:0)
num .* pow2(1-nBit:0)
floor(num .* pow2(1-nBit:0))

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2017a

질문:

2022년 3월 7일

편집:

Jan
2022년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by