How to return 'X' number of unique subsets (combinations) of 'N' numbers taken 'K' at a time
6 views (last 30 days)
Show older comments
John Trimper
on 23 Jul 2015
Edited: Walter Roberson
on 27 Jul 2015
I need to return X number of unique combinations of N numbers (i.e., vector V of length N) taken K at a time.
I can't use 'nchoosek' because I don't want ALL unique combinations. I just want X number of them and 'nchoosek' will crash if I enter the actual values for V and K because V is too large.
Here's an example, with more descriptive variable names…
origSet = rand(1,500); %the full original (example) set of numbers
desNumComb = 10000; %the number of unique combinations/subsets that I want to end up with
subsetSize = 10; %the desired size for each combination/subset
allCombos = nchoosek(1:length(origSet), subsetSize); %will return ALL possible combinations (if it ran)
subsetInds = allCombos(desNumComb,:); %the indices for each of the desNumComb subsets
Worth mentioning is that the size of the original set of numbers [i.e., length(origSet) ], the desired subset size [i.e., subsetSize], and the desired number of unique combinations [i.e., desNumComb] will possibly vary every time I loop through, which will be many times.
Thanks in advance to all.
Cheers, John
2 Comments
Walter Roberson
on 23 Jul 2015
Which X subsets? The "first" X subsets under some specific ordering? X random subsets? Are you using this to iterate through all the possibilities in batches?
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!