我想用10个子集计算1:16的所有可能组合。
combos = combntns(1:16,10)
但条件是返回的组合应该具有以下向量中的最小1个成员:
V1=1:4,V2=5:8,V3=9:12,V4=13:16,
任何解决方案?
答案 0 :(得分:2)
使用该问题大小,您可以负担生成所有组合,然后选择符合要求的组合:
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,:);
这可以推广到泛型元素和任意条件向量,假设所有向量的大小相同:
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,:);