【问题标题】:Generating all combinations without repetition using MATLAB使用 MATLAB 生成所有组合而不重复
【发布时间】:2015-05-02 12:08:44
【问题描述】:

我有 4 个集合,每个集合包含 6 个元素,我想从中生成所有可能的大小为 8 的向量,前两个元素来自 set1 第二个 2 来自 set2 第三个 2 来自 set3 第四个 2 来自 set4 没有重复的点取自每个集合使得元素 1,2 / 3,4 / 5,6/ 7,8 总是不同的。我的目标数字组合是 (6choose2)^4 。请帮忙。

    D1=[2+2i,2+1i,1+2i,1+1i,2,1i];
    D2=[-2+2i,-2+1i,-1+2i,-1+1i,-1,2i];
    D3=[-2-2i,-2-i,-1-i,-1-1i,-2,-1i];
    D4=[2-2i,2-i,1-2i,-1+1i,1,-2i];

【问题讨论】:

  • @Hoki 这个链接从每个向量中取 1 个元素我想取 2 个元素而不重复!!
  • 这就是Hoki说“可以帮助你”的原因。您是否尝试修改它以适合您的应用程序?
  • @Benoit_11 修改这样的函数不是一件简单的事情!为什么不尝试这样做并向我们展示您的结果?
  • matlab 已经有choose 函数但是rosetta code page 包含可以在需要时移植到matlab 的代码

标签: matlab combinations


【解决方案1】:

所以我找到了一种方法来获取您的组合。你真的应该给出一个更简单的例子来解释你的问题(这就是我顺便解决的方法)。

程序是:

  • 获取每组的所有 {2 element} 唯一组合。
  • 然后为您获得的结果建立一个索引。通常每个子集都应该有一个索引,但由于它们的长度相同,因此唯一组合的数量将相同,因此您可以重复使用 4 倍相同的索引。
  • 获取这4组索引的所有组合
  • 最后,根据索引组合重建最终矩阵

代码如下:

%// prepare a few helper numbers
nSets = 4 ;
nElemPerSet = 2 ;
nCombs =  nchoosek( numel(D1) ,nElemPerSet).^nSets ; %// <= nCombs=50625

%// for each set, get the unique combinations of 2 elements
s1 = nchoosek( D1 , nElemPerSet ) ;
s2 = nchoosek( D2 , nElemPerSet ) ;
s3 = nchoosek( D3 , nElemPerSet ) ;
s4 = nchoosek( D4 , nElemPerSet ) ;

%// now get the index of all the combinations of the above subsets
s = 1:size(s1,1) ;
combindex = all_combinations( repmat({s},1,4) ) ; %// <= size(combindex)=[50625 4]

%// now rebuild the full combinations based on above indices
combinations = zeros( nCombs , nSets*nElemPerSet ) ;
for ic = 1:nCombs
    combinations(ic,:) = [s1(combindex(ic,1),:) s2(combindex(ic,2),:) s3(combindex(ic,3),:) s4(combindex(ic,4),:)] ;
end

可能有一种方法可以通过智能使用 arrayfun 来摆脱最后一个循环,但我将其作为练习留给读者。

此代码适用于您在问题中描述的 D1, D2, D3 and D4 的初始值,但如果您或任何人想逐步运行它以了解正在发生的事情,我强烈建议您尝试使用更简单的起始值。比如:

%// define 4 non-complex sets of 4 values each (all different)
nVal=4 ;
D1 = 1:nVal ;
D2 = D1(end)+1:D1(end)+nVal ;
D3 = D2(end)+1:D2(end)+nVal ;
D4 = D3(end)+1:D3(end)+nVal ;

注意函数all_combinations的使用。这只是我在重新打包在函数中的评论 (Generate a matrix containing all combinations of elements taken from n vectors) 中提到的答案。如果您经常处理组合问题,我建议您查看并添加书签(如果它对您有帮助,您也可以对其进行投票,这里就是这样做的)。

重新封装的功能是:

function combs = all_combinations( vectors )
%// function combs = all_combinations( vectors )
%//
%// example input :
%//     vectors = { [1 2], [3 6 9], [10 20] }; %//cell array of vectors
%//
%// Credit: Luis Mendo : https://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors

n = numel(vectors);             %// number of vectors
combs = cell(1,n);              %// pre-define to generate comma-separated list
[combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %// the reverse order in these two
%// comma-separated lists is needed to produce the rows of the result matrix in
%// lexicographical order 
combs = cat(n+1, combs{:});     %// concat the n n-dim arrays along dimension n+1
combs = reshape(combs,[],n);    %// reshape to obtain desired matrix

【讨论】:

    猜你喜欢
    • 2015-04-01
    • 2017-05-19
    • 2012-02-12
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多