About this blog

I feel this blog as a reflection of my thoughts to myself , and sometimes as a public diary, and the is my only friend to share my thoughts who says never a "oh no! ,you shouldn't....That is boring...."

N choose n Matlab Octave program

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

కామెంట్‌లు లేవు: