from from decimal to two's complement by Moatasem Chehaiber
a function to convert a decimal number into 2's complement number of defined size

vector=dec2bit(x,size)
% dec2bit
%converts a real number between -1.0 and 1.0 into a tow's complementary representation
%useful in fixed point calculations.
function vector=dec2bit(x,size)
if x< 0.0 
tmp = 2- abs(x);
else 
    tmp=abs(x);
end;
	 for i = 1:size  
	     if tmp >=1.0  
	      vector(size+1-i)='1';
			tmp= tmp-1.0;
			 else 
			  vector(size+1 -i)='0';
         end;
         tmp = 2.0 * tmp;
     end 
	 

Contact us at files@mathworks.com