[英]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: 您可以像这样将cellfun
和bsxfun
结合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
. 如果必须按照问题的顺序排列它们,请尝试使用flipud
或fliplr
。 An alternative to bsxfun
is repmat
: bsxfun
的替代bsxfun
是repmat
:
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.