title: Randomly select subset of all possible combinations in Matlab?
body: <p>I need to select random combinations of k elements from a set of n elements, where n can be fairly large. Given the size of the set, it is not feasible to simply use <code>combnk</code> or <code>nchoosek</code> to generate all possible combinations, and select randomly from those.</p>
  
  <p>Is there an easy way to generate a unique random subset of M of those combinations?</p>
  
  <p>When n is small, the following works:</p>
  
  <pre><code>M = 20; %want to pick M random combinations
  n = 10; %number of elements 
  k = 5;  %number of elements in each combination
  allCombos = nchoosek([1:n], k);  %for large n this is not feasible
  numCombos = nchoosek(n,k);
  permutationsToUse = randperm(numCombos, M);
  randomCombos = allCombos(permutationsToUse, :);
  </code></pre>
  
  <p>When n is large, this is no longer feasible.</p>
  
  <p><strong>Related Posts</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/29077193/retrieve-a-specific-permutation-without-storing-all-possible-permutations-in-mat">Retrieve a specific permutation without storing all possible permutations in Matlab</a></p>
  
  <p><a href="https://stackoverflow.com/questions/27125415/how-to-randomly-pick-a-number-of-combinations-from-all-the-combinations-efficien">How to randomly pick a number of combinations from all the combinations efficiently?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18292144/select-a-subset-of-combinations">Select a subset of combinations</a></p>