Asked by jm
How to convert from decimal to binary in matlab.....this is what I have so far.....
dec = 43;
i = 1;
sum=0;
while dec>0
r = rem(dec, 2);
sum=sum+(i*rem);
dec=dec/2;
i=i*10;
end
sum
dec = 43;
i = 1;
sum=0;
while dec>0
r = rem(dec, 2);
sum=sum+(i*rem);
dec=dec/2;
i=i*10;
end
sum
Answers
Answered by
MathMate
The logic looks correct except for one minor logic error in:
sum=sum+(i*rem);
which should read
sum=sum+i*r;
You need to make sure the statements execute correctly in Matlab syntax.
For example, an integer division can be written as
dec=dec./2;
or
dec=idivide(dec,2);
Also, be careful about overflow of the decimal representation of the binary number (sum), which has at least 3 times more digits as the decimal equivalent.
sum=sum+(i*rem);
which should read
sum=sum+i*r;
You need to make sure the statements execute correctly in Matlab syntax.
For example, an integer division can be written as
dec=dec./2;
or
dec=idivide(dec,2);
Also, be careful about overflow of the decimal representation of the binary number (sum), which has at least 3 times more digits as the decimal equivalent.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.