[MOVED] My function to convert decimal numbers into binary numbers using Matlab

Status
Not open for further replies.

vickyuet

Member level 2
Joined
Oct 3, 2006
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
pakistan
Visit site
Activity points
1,691
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....
 

Re: My function to convert decimal numbers into binary numbers using Matlab

hi! im new with MATLAB. and i really find it hard to understand the commands you've used. but i already tried that and it works perfectly. could you please explain it a bit more? (i dont know what those f, len_num and fliplr means :???: ) thanks in advance!
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…