vickyuet
Member level 2
To convert numbers ,I had written my own function in Matlab instead of dec2bin or bin2dec(because it returns strings).I had checked it for many test cases and found it working.Can any body confirm, it works for all the valid test cases.I mean to cater any logical mistake at this stage because I need to apply it for large chunk of data.....for which verifying the accuracy will be difficult... valuable suggestions for improvement required...
function [out]=my_dec2bin( num )
num = round (num) ; % To ensure an integer number is entered
[f , len_num]=log2(max(num)) % log computes bits required for representation of max decimal value
for i=1:len_num
if num < 0
error('Number must be positive');
elseif num >= 1
remainder(1,i) = rem ( num , 2)
num = fix(num / 2); % convert it 2 integer again after division
end
end
out = fliplr(remainder(1 , ); % flip to get right answer
end % function ends here....
function [out]=my_dec2bin( num )
num = round (num) ; % To ensure an integer number is entered
[f , len_num]=log2(max(num)) % log computes bits required for representation of max decimal value
for i=1:len_num
if num < 0
error('Number must be positive');
elseif num >= 1
remainder(1,i) = rem ( num , 2)
num = fix(num / 2); % convert it 2 integer again after division
end
end
out = fliplr(remainder(1 , ); % flip to get right answer
end % function ends here....