Write a function in matlab that could turn a decimal to bina
Write a function in matlab that could turn a decimal to binary and works for fractional numbers. ( like 24.4)
Solution
Hello everyone;
I need to write a easy programme which take inputs of a decimal integer or decimal fractions (positive or negative) of a matrix and a bit value that want to be converted from decimal to binary than back decimal value.
I have written below a programme for just one value:
This programme works for just a value but I couldn\'t bring the matrice values one by one then combine it.
For easy understand I want to explain with an example: Let f= [0.128 -1.35 0.489 3.547]
For more clearity please look into the below examples
For example: Let A= [0.254 -1.569 4.624 0.147]
First we will convert these decimal numbers to binary. It gives approximately: Abin=[0.01000001000001100010010011011101 -1.10010001101010011111101111100111 100.10011111101111100111011011001000 0.00100101101000011100101011000000]
Now let\'s first take 4-bits of binary means:
Abin4=[0.0100 -1.1001 100.1001 0.0010]
Then convert back to decimal it gives: Adec4=[0.2500 -1.5625 4.5625 0.125]
As we see there are differences (errors) between the new converted and the original. I want to do these process. But not only 4-bits, for any bit number. When the bit number increases the error gets lower. I will show that.
