### 2.52 generate random matrix so that each column adds to 1

2.52.1 Mathematica
2.52.2 Matlab
2.52.3 Maple

Generate the random matrix and divide each column by its total

#### 2.52.1 Mathematica

 This method due to Bob Hanlon from the Math group mat = Table[RandomReal[],{3},{4}] s = Total[mat,{1}] b = #/s& /@ mat Total[b,{1}]  Out[31]= {{0.440393,0.945076,0.0527301,0.537288},           {0.515868,0.565691,0.800959,0.0302484},           {0.509004,0.143124,0.519455,0.264779}} Out[32]= {1.46526,1.65389,1.37314,0.832315} Out[36]= {{0.300555,0.571426,0.038401,0.645535},           {0.352065,0.342036,0.583303,0.0363425},           {0.34738,0.0865376,0.378296,0.318123}} Out[37]= {1.,1.,1.,1.} Or can use Transpose b = Transpose[Transpose[mat]/s]; Total[b,{1}]  {1.,1.,1.,1.}  Another way of doing the above, without the need to transpose 2 times is the following b=Inner[Divide,mat,s,List]; Total[b,{1}]  {1.,1.,1.,1.} 

#### 2.52.2 Matlab

 A=rand(3,4) s=sum(A,1); B=bsxfun(@rdivide,A,s) sum(B,1)  A =     0.6948    0.0344    0.7655    0.4898     0.3171    0.4387    0.7952    0.4456     0.9502    0.3816    0.1869    0.6463 B =     0.3541    0.0403    0.4380    0.3097     0.1616    0.5133    0.4550    0.2817     0.4843    0.4464    0.1069    0.4086 ans =     1.0000    1.0000    1.0000    1.0000

#### 2.52.3 Maple

A:=LinearAlgebra:-RandomMatrix(3,4);


     [ 9  -95   51  24]
[                ]
A := [99  -20   76  65]
[                ]
[60  -25  -44  86]


b:=MTM:-sum(A,1);  #sum columns
b := [168, -140, 83, 175]


nCol:=LinearAlgebra:-ColumnDimension(A):
B:=convert([seq(A[..,i]/b[i],i=1..nCol)],Matrix);



     [3   19   51  24 ]
[--  --   --  ---]
[56  28   83  175]
[                ]
[33   1   76   13]
B := [--   -   --   --]
[56   7   83   35]
[                ]
[5   5   -44  86 ]
[--  --  ---  ---]
[14  28  83   175]
MTM:-sum(B,1);


  [1, 1, 1, 1]