title: Generating all combinations with repetition using MATLAB
body: <p>How do I create all <a href="http://en.wikipedia.org/wiki/Combination#Number_of_combinations_with_repetition" rel="noreferrer"><em>k-combinations with repetitions</em></a> of a given set (also called <em>k-multicombinations</em> or <em>multisubsets</em>) using MATLAB? </p>
  
  <p>This is similar to the cartesian product, but two rows that only differ by their sorting should be considered the same (e.g. the vectors <code>[1,1,2]=~=[1,2,1]</code> are considered to be the same), so generating the cartesian product and then applying <code>unique(sort(cartesianProduct,2),'rows')</code> should yield the same results.</p>
  
  <p><strong>Example:</strong>
  The call <code>nmultichoosek(1:n,k)</code> should generate the following matrix:</p>
  
  <pre><code>nmultichoosek(1:3,3)
  ans =
       1     1     1
       1     1     2
       1     1     3
       1     2     2
       1     2     3
       1     3     3
       2     2     2
       2     2     3
       2     3     3
       3     3     3
  </code></pre>