Matlab fractions into binary fractions help

4 views (last 30 days)
Trevor Frederick
Trevor Frederick on 9 Feb 2016
Edited: dpb on 10 Feb 2016
I know the binary code for the number 13.6094 is 1101.1001110 and I've created 2 functions that can convert the integer part of the number (13) to binary as well as the fractional part (0.6094). The output for the integer part is the vector [1 1 0 1] and the fractional part is [1 0 0 1 1 0]. I now want to know how I can take these vectors and combine them with a decimal point so the code will output 1101.1001110

Answers (2)

Walter Roberson
Walter Roberson on 9 Feb 2016
No, unless you mean as a string. Or unless you want to design a custom MATLAB class that has its own display format.

dpb
dpb on 9 Feb 2016
Edited: dpb on 10 Feb 2016
>> i=[1 1 0 1]; f=[1 0 0 1 1 0];
>> sprintf([repmat('%d',1,length(i)) '.' repmat('%d',1,length(i))],i,f)
ans =
1101.100110
>>
or,
>> [sprintf('%d',i) '.' sprintf('%d',f)]
ans =
1101.100110
>>
instead of building counted format string.
ADDENDUM
Using a lookup table is probably fastest...
>> c=['0' '1' '.']; % the characters to be output
>> [c(i+1) '.' c(f+1)] % would be nice to be able to have 0-based arrays...
ans =
1101.100110
or, build the full number first--
>> d=2; % mnemonic for decimal location in lookup table
>> n=[i 2 f]; % make the full number as integer.fractionc
>> c(n+1)
ans =
1101.100110
>>
Or, of course, you don't have to actually have the temporary...
>> c([i d f]+1)
ans =
1101.100110
>>

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!