Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 根据具体情况找出可能的组合_Arrays_Matlab_Combinations_Combinatorics - Fatal编程技术网

Arrays 根据具体情况找出可能的组合

Arrays 根据具体情况找出可能的组合,arrays,matlab,combinations,combinatorics,Arrays,Matlab,Combinations,Combinatorics,我想用10个子集计算1:16的所有可能组合 combos = combntns(1:16,10) 但条件是返回的组合应至少有以下向量中的1个成员: V1=1:4,V2=5:8,V3=9:12,V4=13:16, 有什么解决方案吗?根据问题大小,您可以生成所有组合,然后选择满足要求的组合: n = 16; %// number of elements to choose from c = 10; %// combination size s = 4; %// size of each

我想用10个子集计算1:16的所有可能组合

combos = combntns(1:16,10)
但条件是返回的组合应至少有以下向量中的1个成员:

V1=1:4,V2=5:8,V3=9:12,V4=13:16,

有什么解决方案吗?

根据问题大小,您可以生成所有组合,然后选择满足要求的组合:

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,:);

很好的解决方案,但如果任何V向量发生变化,这将失败。。。我不知道这是否可能,我想可能不是。我正在研究随机V向量的一般情况的解决方案,但我无法避免一个循环(这使它变得不那么有趣)。我现在已经关闭了MATLAB,因为我想这解决了OP的问题=)@StewieGriffin谢谢!可以对任意向量执行此操作,只要它们的大小相同。我补充说。。。我没有运行MATLAB,但我猜这个条件也一定是真的
mod(numel(元素),向量{k})==0
。不管怎样,(几乎)一般向量的很好的解决方案)=@StewieGriffin你是说
mod(numel(元素),numel(向量{1}))==0
?那真的没必要。例如,我用
vectors={1:3,6:8,9:11,12:14}
进行了检查,结果是。。。。所有向量的大小必须相等,因为
cat(…)
。如果它是1:17的组合,这是一种可能的情况(例如,最后一个向量可能是13:17,或者仅仅是17本身。尽管我在这里吹毛求疵,我想这解决了OP的问题。