(* The following constructs the design matrix for the model
of no three-way interaction. *)

{r1,r2,r3}={3,3,2};
tuples=Flatten[Outer[List,Range[r1],Range[r2],Range[r3]],2];
tuples1=Flatten[Outer[List,Range[r2],Range[r3]],1];
tuples2=Flatten[Outer[List,Range[r1],Range[r3]],1];
tuples3=Flatten[Outer[List,Range[r1],Range[r2]],1];

A1=Outer[If[#1==#2[[{2,3}]],1,0]&,tuples1,tuples,1];
A2=Outer[If[#1==#2[[{1,3}]],1,0]&,tuples2,tuples,1];
A3=Outer[If[#1==#2[[{1,2}]],1,0]&,tuples3,tuples,1];

A=Join[A1,A2,A3];

string[l_]:=Apply[StringJoin,Map[(ToString[#]<>" ")&,l]];

file = OpenWrite["no3way.mat", PageWidth -> Infinity];
WriteString[file,string[Dimensions[A]]<>"\n"];
Map[WriteString[file,(string[#]<>"\n")]&,A];
Close[file]
