MDiffElementEST[matrixIN_, varno_]:=
(* Returns the list of the different elements in the (varno) .  row  of the specified matrixIN 
For data stored like :
 ({{"Math", "Hist", "CS", "Math", "Math", "CS", "Hist", "Math"}, {1, 0, 1, 0, 0, 1, 0, 1}, {1975, 1992, 1982, 1990, 1977, 1984, 1923, 1990}, {"Math", "Math", "Hist", "Hist", "CS", "GYM", "ART", "Hist"}})

NOTE :  
                If your data are such that different variable values reside in cols instead of rows, just feed the Transpose[] of your matrix!


            Emre S Tasci  -  16/11/2007 TUDelft
*)
Module[
{varlist = {}, 
            i}, 
 (* External Functions Used :  
                  NONE;
 *) 
For[
            i = 1, i ≤ 
              Length[matrixIN[[varno]]], i++, 
If[
                MemberQ[
                  varlist, matrixIN[[varno]][[i]]], , 
varlist = 
                    Append[varlist, matrixIN[[varno]][[i]]];
];
]; 
Return[
            varlist];
];

\(\(MPolluteEST[mx_, row_, ratio_] := 
        Module[
 (*
Assigns\ a\ 0\ or\ 1\ \((with\ equal\ probabilities)\)\ to\ the\ randomly\ selected\ "ratio" 
              of\ the\ elements\ of\ the\ matrix\ "mx"\ and\ returns\ the\ new\ "polluted"\ matrix . \ If\ "mx"\ is\ a\ List, \ 
            then\ row\ is\ not\ \(\(relevant\)\(.\)\); 

Emre\ S\ Tasci\  - \ \(07/12\)/
                  2007\ TUDelft;
*) 
{size, num, selected = {}, rand, mx2 = mx, 
            dimensions}, 
dimensions\  = \ \(Dimensions[
                Dimensions[mx]]\)[\([1]\)]; 
If[
            dimensions == 2, 
size = 
              Length[mx[\([row]\)]], 
size = 
              Length[mx]
]; 
num\  = \ 
            Round[size\ *ratio]; 
For[j = 1, 
            j ≤ num, \(j++\), 
Label[
              rando]; 
rand = 
              Random[Integer, {1, size}]; 
If[
              MemberQ[selected, rand], 
              Goto[rando], 
\(selected = 
                  Append[selected, 
                    rand];\)
];
]; 
If[
            dimensions == 
              2, 
\(For[j = 1, 
                j ≤ 
                  num, \(j++\), 
\(mx2[\([row, 
                        selected[\([j]\)]]\)] = 
                    Random[Integer];\)
];\), 
\(For[j = 1, 
                j ≤ 
                  num, \(j++\), 
\(mx2[\([selected[\([j]\)]]\)] = 
                    Random[Integer];\)
];\)
]; 
Return[
            mx2];
];\)\)

MSelectEST[MatrixIN_, varno1_, varval1_,  varno2_]:=Module[
(* Returns the list of "varno2" variables from the "MatrixIN" where "varno1" values equal to "varval1"

For data stored like :
 ({{"Math", "Hist", "CS", "Math", "Math", "CS", "Hist", "Math"}, {1, 0, 1, 0, 0, 1, 0, 1}, {1975, 1992, 1982, 1990, 1977, 1984, 1923, 1990}, {"Math", "Math", "Hist", "Hist", "CS", "GYM", "ART", "Hist"}})

NOTE :  
                      If your data are such that different variable values reside in cols instead of rows, just feed the Transpose[] of your matrix!


              Emre S Tasci  -  16/11/2007 TUDelft

*)
{selPos = {}, i = 1},
  
  
selPos  =  
                Position[MatrixIN[[varno1]], varval1];

  

(* This is how is done with using the AppendRows[] function from the  LinearAlgebra`MatrixManipulation` package: *)
  
  

  

For[i = 1, 
                i ≤ Length[selPos], i++, 
                selPos  = Insert[selPos, varno2, {i, 2}]];

  
selPos  =  Map[Reverse, selPos];
Return[
                Extract[MatrixIN, selPos]];
];

\(\(\(MSwapEST[MatrixIN_, row1_, row2_] := 
        Module[
 (*\ 
            Swaps\ the\ rows\ "row1"\ and\ "row2"\ in\ matrix\ "MatrixIN"\ 
Emre\ S\ Tasci\  - \ \(20/11\)/
                  2007\ TUDelft

\(External\ Functions\ \(Used : \ 
                      NONE\);\)
*) 
{DummyMatrixIN = MatrixIN}, 
{DummyMatrixIN[\([row1]\)], 
              DummyMatrixIN[\([row2]\)]}\  = \ {DummyMatrixIN[\([row2]\)], 
              DummyMatrixIN[\([row1]\)]}; 
Return[
            DummyMatrixIN];
];\)\(
\)
    \)\)