1

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

  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.

bzak
  • 563
  • 1
  • 8
  • 21
  • 1
    Define the line number as `Lines = { [1 2 3], [1 2], [1 2] }` then use this [solution](http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors) to get all combinations. The result gives you which line of each matrix is to be set in the final matrix. – Hoki Jan 08 '15 at 19:38
  • @Hoki: I look for a solution that gives a result from A, because the content of A is variable! – bzak Jan 08 '15 at 19:44

2 Answers2

1

You can do this combining cellfun and bsxfun like this:

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. An alternative to bsxfun is repmat:

B = cellfun(@(n) 100*eye(numel(n))+repmat(n,numel(n),1), A, 'UniformOutput',false)
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • :sorry, what I want is a matrix of 3x2x2 = 12 lines, formed from all different possible combinations of lines from the 3 matrices found by your code. – bzak Jan 08 '15 at 18:56
1

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
Community
  • 1
  • 1
Hoki
  • 11,637
  • 1
  • 24
  • 43
  • Thank you for your answer. I don't program every day, so sometimes I can't find solutions even for a simple problem! – bzak Jan 08 '15 at 20:41
  • in the last loop, can I replace the content with: C(ii,:) = cellfun(@(x)x(combs(ii,1:end),B{1,1:end})); ? – bzak Jan 09 '15 at 00:03
  • @bzak, I don't think that will work as it is. See edit for different output format. – Hoki Jan 09 '15 at 10:08
  • I hope to find a general expression for C(ii:) because A is a variable cell, and therefore also its dimensions. – bzak Jan 09 '15 at 10:28
  • @bzak, there is probably a shorter solution, but in the meantime you could just add another loop (on the number of element of `A` inside to build each line column by column (instead of building the full line as it is now). – Hoki Jan 09 '15 at 10:53
  • I did this (loop) for the other parts of the code but I was not able to do this for C(ii:) – bzak Jan 09 '15 at 10:59
  • @bzak, in which format do you want the result (array? , cell array ?), which of the proposed format (C, D, or E in my example) do you like most ? – Hoki Jan 09 '15 at 11:06
  • matrix C, as at the end of your answer – bzak Jan 09 '15 at 11:09
  • @bzak, I generalized the answer, you can put any number of element in `A` now. Consider accepting the solution if it does what you need. – Hoki Jan 09 '15 at 12:21