Please do this using Matlab. This problem has two parts: first, converting a dec
ID: 3789729 • Letter: P
Question
Please do this using Matlab. This problem has two parts: first, converting a decimal number into binary and octal; and second, converting a binary or octal number back into decimal. Make sure to provide proper commenting.
1) Write a series of functions that convert a decimal number into binary and octal.
2) Write a series of functions that convert a binary or octal number back into decimal.
Note: for both parts, check your functions with the following:
10Decimal=1010Binary=12Octal
20Decimal=10100Binary=24Octal
Please do BOTH parts
Explanation / Answer
Answers-
1)
Method 1> By using matlab commands
We can do decimal to binary conversion using the Matlab command 'dec2bin'. For example:
dec_nr = 10;
bin = dec2bin(dec_nr);
Matlab's answer is
bin = 1010
Note that the result is a string in Matlab, not a number.
Method 2-Writing our own code
To automate the process explained above, we need to explain first the commands 'floor', 'rem', 'num2str' and 'fliplr'.
'floor': rounds towards minus infinity.
'rem': remainder after a division.
'num2str': converts numbers to strings.
'fliplr': flips matrix in left/right direction.
The proposed code is this:
dec_nr = 10;
i = 1;
q = floor(dec_nr/2);
r = rem(dec_nr, 2);
bin(i) = num2str(r(i));
while 2 <= q
dec_nr = q;
i = i + 1;
q = floor(dec_nr/2);
r = rem(dec_nr, 2);
bin(i) = num2str(r);
end
bin(i + 1) = num2str(q);
bin = fliplr(bin)
The while-loop goes on until the quotient is less than 2. q is the quotient, r is the remainder and we keep all the remainders in variable (string) 'bin'. The last bit in our number is the MSB and it's the last quotient obtained, the one that was less than 2.
Since the binary digits (bits) are obtained from LSB to MSB, we have to flip the array in the last step, so that we can see the appropriate order.
Other ways to convert desimal value to binary vector are by using following functions-
decimalToBinaryVector(decimalNumber)
decimalToBinaryVector(decimalNumber,numberOfBits)
decimalToBinaryVector(decimalNumber,numberOfBits,bitOrder)
decimalToBinaryVector(decimalNumber,[],bitOrder)
Decimal to Octal-
Method 1- Using a Matlab function
We can get the decimal to octal conversion using the Matlab command 'dec2base', which converts a decimal integer to a base B string.
dec2base(D, B) returns the representation of D as a string in base B. D must be a non-negative integer array smaller than 252 and B must be an integer between 2 and 36.
For example:
dec_nr = 142;
oct = dec2base(dec_nr, 8);
Matlab's answer is
oct = '216'
1st. method: with a Matlab function
We can get the decimal to octal conversion using the Matlab command 'dec2base', which converts a decimal integer to a base B string.
dec2base(D, B) returns the representation of D as a string in base B. D must be a non-negative integer array smaller than 252 and B must be an integer between 2 and 36.
For example:
dec_nr = 142;
oct = dec2base(dec_nr, 8);
Matlab's answer is
oct = '216'
Note that the result is a 3-character string in Matlab, it's not a number.
Method 2: Own code
To automate the process explained above, we need to explain first the commands 'floor', 'rem', 'num2str' and 'fliplr'.
'floor': rounds towards minus infinity.
'rem': remainder after a division.
'num2str': converts numbers to strings.
'fliplr': flips matrix in left/right direction.
The proposed code and example is this:
dec_nr = 142
base = 8;
i = 1;
q = floor(dec_nr/base);
r = rem(dec_nr, base);
oct(i) = num2str(r(i));
while base <= q
dec_nr = q;
i = i + 1;
q = floor(dec_nr/base);
r = rem(dec_nr, base);
oct(i) = num2str(r);
end
oct(i + 1) = num2str(q);
oct = fliplr(oct)
The while-loop goes on until the quotient is less than our base. q is the quotient, r is the remainder and we keep all the remainders in string variable oct. The last digit in our conversion is the most significan digit (MSD) and it's the last quotient obtained, the one that was less than our base.
Since the octal digits are obtained from least significant to most significand digit, we have to flip the array in the last step, so that we can see the number in the appropriate order.
Answer 2)
Binary to decimal-
d =bi2de(b)
d = bi2de(b,flg)
d = bi2de(b,p)
d = bi2de(b,p,flg)
Description
d =bi2de(b) converts a binary row vector b to a nonnegative decimal integer.
d = bi2de(b,flg) converts a binary row vector to a decimal integer, where flg determines the position of the most significant digit.
d = bi2de(b,p) converts a base-p row vector b to a nonnegative decimal integer.
d = bi2de(b,p,flg) converts a base-p row vector to a decimal integer, where flg determines the position of the most significant digit.
Examples-
Generate a matrix that contains binary representations of five random numbers between 0 and 15. Convert the binary numbers to decimal integers.