简体   繁体   English

如何在特定条件下生成n个向量的所有可能组合

[英]How to generate all possible combinations of n vectors following a particular condition

From A = {[2 31 40],[11 17],[5 8]} , And by using this specific combination of the three vectors (add 100 for each element of a vector, without changing other elements): A = {[2 31 40],[11 17],[5 8]} ,并使用三个向量的这种特定组合(对向量的每个元素加100,而不会更改其他元素):

  2   31  140         11  117         5  108
  2  131   40        111   17       105    8
102   31   40

I hope to be able to produce all possible combinations (matrix 12x7) between the different lines of these three matrices. 我希望能够在这三个矩阵的不同行之间产生所有可能的组合(矩阵12x7)。

You can do this combining cellfun and bsxfun like this: 您可以像这样将cellfunbsxfun结合cellfun

B = cellfun(@(n) bsxfun(@plus, n, 100*eye(numel(n))), A, 'UniformOutput', false)

B{:}
ans =
   102    31    40
     2   131    40
     2    31   140

ans =
   111    17
    11   117

ans =
   105     8
     5   108

If you have to have them in the order you had in the question, try flipud or fliplr . 如果必须按照问题的顺序排列它们,请尝试使用flipudfliplr An alternative to bsxfun is repmat : bsxfun的替代bsxfunrepmat

B = cellfun(@(n) 100*eye(numel(n))+repmat(n,numel(n),1), A, 'UniformOutput',false)

You keep asking questions very closely related, and getting answers for them, you should try to connect all the bits by yourself. 您一直在问非常紧密相关的问题,并获得答案,您应该尝试自己将所有方面联系起来。

In this case, start with the excellent answer from Horchler, inject this result in the (just as excellent) answer from Luis Mendo to get all possible combinations (which you were given many times in your several questions), then build your final matrix. 在这种情况下,请从霍希勒的出色答案开始,将此结果注入路易斯·门多的(同样出色) 答案中 ,以得到所有可能的组合(在几个问题中多次给出该组合),然后构建最终矩阵。

In practice: 在实践中:

%% // initial data
A = {[2 31 40],[11 17],[5 8]} ;

%% // count a few things before we start
nBlock = numel(A) ;                 %// number of elements sub matrix in "A"
nElem  = cellfun( @numel , A ) ;    %// number of elements in each sub matrix of "A"
nLines = prod(nElem) ;              %// number of lines in the final matrix

%% // Horchler solution (to get all the sub-matrix you requested)
B = cellfun(@(n) bsxfun(@plus, n, 100*eye(numel(n))), A, 'UniformOutput', false) ;

%% // connect both solution
Lines = arrayfun( @linspace , ones(1,nBlock) , nElem , nElem , 'UniformOutput',false) ;

%% // Luis Mendo solution
% // https://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors
nBlock = numel(Lines);               %// number of vectors
combs = cell(1,nBlock);              %// pre-define to generate comma-separated list
[combs{end:-1:1}] = ndgrid(Lines{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(nBlock+1, combs{:});     %// concat the n n-dim arrays along dimension n+1
combs = reshape(combs,[],nBlock);    %// reshape to obtain desired matrix

%% // Finalisation (can be optimized but it works as it is)
for ii=nLines:-1:1
    tmpLine = [] ;
    for jj=1:nBlock
        tmpLine = [ tmpLine B{1,jj}(combs(ii,jj),:) ] ; %// %#ok<AGROW>
    end
    C(ii,:) = tmpLine ;
end

gives you 给你

>> C
C =
   102    31    40   111    17   105     8
   102    31    40   111    17     5   108
   102    31    40    11   117   105     8
   102    31    40    11   117     5   108
     2   131    40   111    17   105     8
     2   131    40   111    17     5   108
     2   131    40    11   117   105     8
     2   131    40    11   117     5   108
     2    31   140   111    17   105     8
     2    31   140   111    17     5   108
     2    31   140    11   117   105     8
     2    31   140    11   117     5   108

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 MATLAB 中为矩阵的列向量生成所有可能的组合? - How to generate all possible combinations for the column vectors of a matrix, in MATLAB? 如何生成所有可能的组合n位字符串? - How to generate all possible combinations n-bit strings? 生成一个矩阵,其中包含取自 n 个向量的元素的所有组合 - Generate a matrix containing all combinations of elements taken from n vectors 生成某些向量元素的所有可能组合(笛卡尔积) - Generate all possible combinations of the elements of some vectors (Cartesian product) Matlab:如何从2个向量中找到所有可能的组合? - Matlab: How to find all possible combinations from 2 vectors? 生成2个向量的所有可能排列 - Generate all possible permutations for 2 vectors 元素/向量/向量列表的所有可能组合(笛卡尔积) - All possible combinations of the elements/vectors/list of vectors (Cartesian product) 如何确定随机向量的所有组合? - How to determine all combinations of a random number of vectors? 在Matlab中生成Matrix的所有可能组合 - Generate All Possible combinations of a Matrix in Matlab MATLAB:如何在输入向量的所有可能组合上评估具有多个输入的函数 - MATLAB: How to evaluate a function with multiple inputs on all possible combinations of input vectors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM