Reputation: 3405
I want to calculate all possible combinations of 1:16 with 10 subsets.
combos = combntns(1:16,10)
But the condition is that the returned combos should have minimum 1 member of the following vectors:
V1=1:4,V2=5:8,V3=9:12,V4=13:16,
Any solution?
Upvotes: 0
Views: 109
Reputation: 112749
With that problem size you can afford to generate all combinations and then select those that meet the requirements:
n = 16; %// number of elements to choose from
c = 10; %// combination size
s = 4; %// size of each group (size of V1, V2 etc)
combos = nchoosek(1:n, c);
ind = all(any(any(bsxfun(@eq, combos, reshape(1:n, 1,1,s,[])),2),3),4);
combos = combos(ind,:);
This can be generalized for generic elements and arbitrary condition vectors, assuming all vectors are the same size:
elements = 1:16; %// elements to choose from
c = 10; %// combination size
vectors = {1:4, 5:8, 9:12, 13:16}; %// cell array of vectors
s = numel(vectors{1});
combos = nchoosek(elements, c);
ind = all(any(any(bsxfun(@eq, combos, reshape(cat(1,vectors{:}).', 1,1,s,[])),2),3),4); %'
combos = combos(ind,:);
Upvotes: 2