matlab - All possible combinations of the elements/vectors/list of vectors (Cartesian product) -


i have several vectors or lists of vectors , make possible concatenations of entries. here example:

a1=4; a2=[1,6;1,9;6,9]; a3=[2;7]; 

that should result in:

[4,1,6,2] [4,1,6,7] [4,1,9,2] [4,1,9,7] [4,6,9,2] [4,6,9,7] 

i think question similar one: generate possible combinations of elements of vectors (cartesian product) not able adapt answers problem.

edit: thank again answer , corrections! beaker said works charm in octave. have little more flexible arbitrary number of a's combined in cell array (or other structure fit potential solution better). made work around composing string , evaling it. not seem pretty elegant me. possible have more... algorithmic?

i answering using matlab in hope same code works in octave well.


here's solution based on amro's answer in question linked:

a1=4; a2=[1,6;1,9;6,9]; a3=[2;7];  nrows = [size(a1,1), size(a2,1), size(a3,1)];  [x,y,z] = ndgrid(1:nrows(1),1:(nrows(2)),1:(nrows(3))); cartprod = [a1(x(:),:) a2(y(:),:) a3(z(:),:)]; 

which results in:

cartprod =       4     1     6     2      4     1     9     2      4     6     9     2      4     1     6     7      4     1     9     7      4     6     9     7 

it's different order showed, think still useful you.


Comments

Popular posts from this blog

javascript - React Maximum Callstack when adding component -

javascript - Uncaught FirebaseError: Messaging: This method is available in a Window context -

correlation - Autocorrelation in Matlab WITHOUT xcorr -