Combinations of the rows of matrices

1 view (last 30 days)
Jason
Jason on 7 Jul 2015
Edited: James Tursa on 7 Jul 2015
I am trying to find all possible combinations of matrices a1-a3.
a1 = [1 2 3; 4 5 6];
a2 = [7 8; 9 10];
a3 = [11; 12];
I want the resulting matrix.
[1 2 3 7 8 11;
1 2 3 9 10 11;
1 2 3 7 8 12;
1 2 3 9 10 12;
4 5 6 7 8 11;
4 5 6 9 10 11;
4 5 6 7 8 12;
4 5 6 9 10 12]

Answers (1)

James Tursa
James Tursa on 7 Jul 2015
Edited: James Tursa on 7 Jul 2015
A slightly different order for the rows than you have listed, but contains the same content:
[X Y Z] = meshgrid(1:size(a1,1),1:size(a2,1),1:size(a3,1));
result = [a1(X(:),:) a2(Y(:),:) a3(Z(:),:)];

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!