如何根据特定条件生成 n 个向量的所有可能组合
How to generate all possible combinations of n vectors following a particular condition
来自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
我希望能够在这三个矩阵的不同行之间产生所有可能的组合(矩阵12x7)。
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
如果您按照问题中的顺序排列它们,请尝试flipud
or fliplr
. An alternative to bsxfun
is repmat
:
B = cellfun(@(n) 100*eye(numel(n))+repmat(n,numel(n),1), A, 'UniformOutput',false)
你一直在问非常相关的问题,并得到答案,你应该尝试自己把所有的部分联系起来。
在这种情况下,从 Horchler 的出色答案开始,将此结果注入 Luis Mendo 的(同样出色的)answer 以获得所有可能的组合(您在几个问题),然后构建你的最终矩阵。
实践中:
%% // 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
% //
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
给你
>> 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
来自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
我希望能够在这三个矩阵的不同行之间产生所有可能的组合(矩阵12x7)。
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
如果您按照问题中的顺序排列它们,请尝试flipud
or fliplr
. An alternative to bsxfun
is repmat
:
B = cellfun(@(n) 100*eye(numel(n))+repmat(n,numel(n),1), A, 'UniformOutput',false)
你一直在问非常相关的问题,并得到答案,你应该尝试自己把所有的部分联系起来。
在这种情况下,从 Horchler 的出色答案开始,将此结果注入 Luis Mendo 的(同样出色的)answer 以获得所有可能的组合(您在几个问题),然后构建你的最终矩阵。
实践中:
%% // 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
% //
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
给你
>> 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