The following is an Octave/ Matlab program to generate all N chose n choices for given N and n.
function [X]=comb(N, n)
%% Generates all possible combinations for N choose n
X=[];
for i=1:N
X=[X,eye(N)(:,i)];
endfor
for B=1:n
for i=1:size(X,2)
for j=1:N
if(X(j,i)==1)
continue
endif
X=[X, X(:,i)];
X(j,end)=1;
endfor
endfor
[idx]=find(sum(X,1)==B);
X=X(:,idx);
X=unique(X',"rows")';
endfor
size(X,2)
endfunction
%% Generates all possible combinations for N choose n
X=[];
for i=1:N
X=[X,eye(N)(:,i)];
endfor
for B=1:n
for i=1:size(X,2)
for j=1:N
if(X(j,i)==1)
continue
endif
X=[X, X(:,i)];
X(j,end)=1;
endfor
endfor
[idx]=find(sum(X,1)==B);
X=X(:,idx);
X=unique(X',"rows")';
endfor
size(X,2)
endfunction
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి