We can use the bijection mentioned in the wikipedia article, which displays combinations without repeating the type n+k-1 choose k to k -multiple combinations of size n . We generate combinations without repetition and match them using bsxfun(@minus, nchoosek(1:n+k-1,k), 0:k-1); . This results in the following function:
function combs = nmultichoosek(values, k) %// Return number of multisubsets or actual multisubsets. if numel(values)==1 n = values; combs = nchoosek(n+k-1,k); else n = numel(values); combs = bsxfun(@minus, nchoosek(1:n+k-1,k), 0:k-1); combs = reshape(values(combs),[],k); end
source share