I have a code to generate the binary sequence that only contains m zeros and n ones. For example, m=n=2. then the number of combinations of the binary sequence is 6,nchoosek(4,2). My code was fine to output the 6 combinations, but mostly 2 combinations are the same. My question is how to avoid a combination of sequence show twice?
The code is following:
% clear all;
m = input('How many zeroes do you need \n');n = input('how many ones do you need\n');lnk=nchoosek(m+n,n);i=n+m;for kk=1:lnk % m = input('How many zeroes do you need \n');
% n = input('how many ones do you need\n');
if i<=1 disp('size of binary sequence is out of range'); else %Binary sequence contains m zeros and n ones in any order
x1=zeros(1,i); x1(randperm(i,n))=1 %count down the number of switches in such a Binary sequence x1;
switches=0; for k=1:length(x1)-1 if x1(k)==0 && x1(k+1)==1 switches= switches+1; elseif x1(k)==1 && x1(k+1)==0 switches=switches+1; end end Av(kk)=switches; end endAlso, my result shows like:How many zeroes do you need 2how many ones do you need2x1 = 0 0 1 1x1 = 0 0 1 1x1 = 0 1 1 0x1 = 1 0 1 0x1 = 1 1 0 0x1 = 1 1 0 0Av = 1 1 2 3 1 1tot_combination = 6
Best Answer