MATLAB: Problem Related to Scenario Generation.

MATLABmatricesmatrix operationsscenario generation

I want to generate an Output matrix from the Input Matrix such that
Input matrix =
[a1,a2,a3,....;
b1,b2,b3,....;
.............;
.............;
z1,z2,z3,....;
.............]
gives the Output Matrix =
[a1,b1,c1,...,x1,y1,z1;
a1,b1,c1,....,x1,y1,z2;
a1,b1,c1,....,x1,y1,z3;
......................;
a1,b1,c1,....,x1,y2,z1;
a1,b1,c1,....,x1,y2,z2;
a1,b1,c1,....,x1,y2,z3;
......................;
a1,b1,c1,....,x2,y1,z1;
a1,b1,c1,....,x2,y1,z2;
a1,b1,c1,....,x2,y1,z3;
......................;
......................;
a2,b1,c1,.....,x2,y,z1;
......................;
......................;
......................]
Means the elements of same row in INPUT matrix must not be in same row in Output matrix.
and must group with the all the elements of the others rows of input matrix as per the output pattern.
Example
Input Matrix =
[100,60,20;
50,30,10;
700,500,200]
Output Matrix=
[100,50,700;
100,50,500;
100,50,200;
100,30,700;
100,30,500;
100,30,200;
100,10,700;
.
.
20,10,700;
20,10,500;
20,10,200]

Best Answer

The following will work for input matrices of width of 10 or less, due to the usage of dec2base to generate the various column combinations:
in = [100, 60, 20;, 50, 30, 10; 700, 500, 200];
assert(size(in, 2) <= 10, 'input matrix is too wide');
numperms = size(in, 2) ^ size(in, 1);
colselect = dec2base(0:numperms-1, size(in, 2)) - '0' + 1;
rowselect = repmat(1:size(in, 1), numperms, 1);
out = in(sub2ind(size(in), rowselect, colselect))
For larger matrices (but the number of permutations may be prohibitively high), it's possible to generate the combinations with ndgrid:
colcombs = cell(1, size(in, 1));
[colcombs{:}] = ndgrid(1:size(in, 2));
colcombs = cellfun(@(c) c(:), colcombs, 'UniformOutput', false);
colselect = [colcombs{:}];