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 combnk
or nchoosek
to generate all possible combinations, and select randomly from those.
Is there an easy way to generate a unique random subset of M of those combinations?
When n is small, the following works:
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, :);
When n is large, this is no longer feasible.
Related Posts
Retrieve a specific permutation without storing all possible permutations in Matlab
How to randomly pick a number of combinations from all the combinations efficiently?