function x = dec_to_bin(i, NB) %Translates a decimal number to its binary equivalent. %NB= integer, number of bits %i= integer between 0 and 2^NB-1 %x= NB component row vector; % binary representation of i, prepadded with zeros so it has NB bits if (NB<1) error("NB is less than 1"); end if (i>=2^NB | i<0) error("i is out of range"); end x=zeros(1, NB); q=i; for alp=0:(NB-1) index = NB-alp; rem=q-2*floor(q/2); %rem=remainder q=floor(q/2); x(index)=rem; end