Question

Mtivity 4. Fun ah Raary N.abn Free dei ma』to binary: Converting between bases ·To co vert fromdecinal phase 101 to binary: Esame:13 to binary Converting between bases To convert from detimal (base 10) to binary From binary to decimal: Converting between bases .Toconvert from binary to decimal: The decimal vale is Example:1101 1101-1 Converting between bases To convert from binary to decimal: Nole that the input, Nnary rqpresmitation ef an inteper, is a string (an array of characlen) For cxample, atring input 10110, mumeric output 22 he scripts convert decābin script.n and coevert binzdee acripe. stržnum, Need these 2 scripts written on matlab please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

% Matlab file convert_dec2bin.m

% Matlab function to convert decimal number to binary

% Input n - integer

% Output binary = binary representation of n

function [binary] = convert_dec2bin(n)

    binary =''; % binary is a string

    % loop continues till n!=0

    while(n>0)

        rem = mod(n,2); % find the modulus when n is divided by 2

        binary = strcat(num2str(rem),binary);

        n=fix(n/2); % get only the integer part

    end

   

end

% End of convert_dec2bin.m

% Matlab file convert_dec2bin_script.m

% Matlab script calling convert_dec2bin function to convert decimal to

% binary

clc;

x = 24;

fprintf('Binary of %d is %s\n',x,convert_dec2bin(x));

% End of convert_dec2bin_script.m

Output:

Binary of 24 is 11000

% Matlab file convert_bin2dec.m

% Matlab function to convert binary number to decimal

% Input numStr - string containing binary representation of n

% Output decimal - decimal of numStr

function [decimal] = convert_bin2dec(numStr)

    decimal =0;

    % loop continues till we reach end of the string

    for i = 1:length(numStr)

        num = str2num(numStr(i)); % convert the element at i to number

        decimal = decimal + num * power(2,length(numStr)-i);

    end

end

% End of convert_bin2dec.m file

% Matlab file convert_bin2dec_script.m

% Matlab script calling convert_bin2dec function to convert binary to

% decimal

clc;

x = '101010';

fprintf('Decimal of %s is %d\n',x,convert_bin2dec(x));

% End of file convert_bin2dec_script.m

Output:

Decimal of 101010 is 42

Add a comment
Know the answer?
Add Answer to:
Need these 2 scripts written on matlab please Mtivity 4. Fun "ah Raary N.abn Free dei...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT