Write a user-defined MATLAB function that converts integers written in binary fo
ID: 2993702 • Letter: W
Question
Write a user-defined MATLAB function that converts integers written in binary form to decimal form. Name the function d = binaTOint(b), where the input argument b is a vector with 1s and 0s that represents the binary number to be converted and the output argument d is a number in decimal form. The largest number that could be converted with the function should be a binary number with 20 1s. If a larger number is entered for b, the function should display an error message. Use the function to convert the following numbers:
a) 11010
b) 0101100111
c) 11100110001110101
Explanation / Answer
function d=binaTOint(b)
d=0;
if length(b)<=20
for i=1 : length(b)
d=d+ str2num(b(i)) * 2^(lengh(b)-i);
end
d
else
disp('Number size is learger then 20');
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.