Home > utility > cartesianProduct.m

cartesianProduct

PURPOSE ^

SYNOPSIS ^

function out=cartesianProduct(in1, in2, in3, in4)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out=cartesianProduct(in1, in2, in3, in4)
0002 
0003 if nargin<1, selfdemo; return; end
0004 
0005 out={};
0006 if nargin==2
0007     for i=1:length(in1)
0008         for j=1:length(in2)
0009             out{end+1}=[in1{i}, in2{j}];
0010         end
0011     end
0012 end
0013 
0014 if nargin==3
0015     for i=1:length(in1)
0016         for j=1:length(in2)
0017             for k=1:length(in3)
0018                 out{end+1}=[in1{i}, in2{j}, in3{k}];
0019             end
0020         end
0021     end
0022 end
0023 
0024 if nargin==4
0025     for i=1:length(in1)
0026         for j=1:length(in2)
0027             for k=1:length(in3)
0028                 for l=1:length(in4)
0029                     out{end+1}=[in1{i}, in2{j}, in3{k}, in4{l}];
0030                 end
0031             end
0032         end
0033     end
0034 end
0035 
0036 % ====== Self demo
0037 function selfdemo
0038 in1={'I ', 'We ', 'You '};
0039 in2={'are ', 'am '};
0040 in3={'here.', 'there.'};
0041 fprintf('in1=%s\n', cell2str(in1));
0042 fprintf('in2=%s\n', cell2str(in2));
0043 fprintf('in3=%s\n', cell2str(in3));
0044 out=cartesianProduct(in1, in2, in3);
0045 fprintf('out=cartesianProduct(in1, in2, in3)\n');
0046 fprintf('The content of out:\n');
0047 disp(out);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003