function [nextc] = nextComb(oldc)
   nextc = [];
   o = find(oldc, 1);                 %// find the first one
   z = find(~oldc(o+1:end), 1) + o;   %// find the first zero *after* the first one
   if length(z) > 0
      nextc = oldc;
      nextc(1:z-1) = 0;
      nextc(z) = 1;
      nextc(1:nnz(oldc(1:z-2))) = 1;
   else
      nextc = zeros(size(oldc));
      nextc(1:nnz(oldc)) = 1;         %// start over
   end
end

C = [1 2 3 4];
n = 4; k = 2;
nextCombination = zeros(1,n);
nextCombination(1:k) = 1;
allCombs(1,:) = C(find(nextCombination));
for c = 2:nchoosek(n,k)   %// start from 2; we already have 1
   nextCombination = nextComb(nextCombination);
   allCombs(c,:) = C(find(nextCombination));
end
allCombs
