fork download
  1. function [nextc] = nextComb(oldc)
  2. nextc = [];
  3. o = find(oldc, 1); %// find the first one
  4. z = find(~oldc(o+1:end), 1) + o; %// find the first zero *after* the first one
  5. if length(z) > 0
  6. nextc = oldc;
  7. nextc(1:z-1) = 0;
  8. nextc(z) = 1;
  9. nextc(1:nnz(oldc(1:z-2))) = 1;
  10. else
  11. nextc = zeros(size(oldc));
  12. nextc(1:nnz(oldc)) = 1; %// start over
  13. end
  14. end
  15.  
  16. C = [1 2 3 4];
  17. n = 4; k = 2;
  18. nextCombination = zeros(1,n);
  19. nextCombination(1:k) = 1;
  20. allCombs(1,:) = C(find(nextCombination));
  21. for c = 2:nchoosek(n,k) %// start from 2; we already have 1
  22. nextCombination = nextComb(nextCombination);
  23. allCombs(c,:) = C(find(nextCombination));
  24. end
  25. allCombs
  26.  
Success #stdin #stdout #stderr 0.19s 65080KB
stdin
Standard input is empty
stdout
allCombs =

   1   2
   1   3
   2   3
   1   4
   2   4
   3   4

stderr
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/__all_opts__.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/optimset.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fminbnd.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fminbnd.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/set/unique.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/general/isvector.m' is in the future
warning: timestamp on file /usr/lib/i386-linux-gnu/octave/3.6.2/oct/i486-pc-linux-gnu/find.oct is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fminunc.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fminunc.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fminunc.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fsolve.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fsolve.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fsolve.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fsolve.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fzero.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/fzero.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/lsqnonneg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/pqpnonneg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/optimization/qp.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/pkg/pkg.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/miscellaneous/fullfile.m' is in the future
warning: timestamp on file /usr/lib/i386-linux-gnu/octave/3.6.2/oct/i486-pc-linux-gnu/cellfun.oct is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/strings/cstrcat.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/miscellaneous/ispc.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/miscellaneous/fileattrib.m' is in the future
warning: timestamp on file /usr/lib/i386-linux-gnu/octave/3.6.2/oct/i486-pc-linux-gnu/strfind.oct is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/specfun/nchoosek.m' is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/general/isscalar.m' is in the future
warning: timestamp on file /usr/lib/i386-linux-gnu/octave/3.6.2/oct/i486-pc-linux-gnu/max.oct is in the future
warning: time stamp for `/usr/share/octave/3.6.2/m/startup/__finish__.m' is in the future