Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: random matrix from row and column sums

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53727] Re: random matrix from row and column sums
  • From: "Simons, F.H." <F.H.Simons at tue.nl>
  • Date: Tue, 25 Jan 2005 05:03:27 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Here is (another) very fast function for finding a matrix of non
negative integers with given row and column sums. We start with a random
matrix of non negative integers. Then we redefine the first row and the
first column in such a way that the matrix has the desired row and
column sums, as done by Astanoff in his second posting on this topic. As
a consequence, some of the elements on the first row or first column now
can be negative. To overcome this, we manipulate the matrix in such a
way that the row and column sums do not change and negative entries
become at least 0.

randommatrix[rowSums_, colSums_] := Module[{mat, neg, a,b},
    mat =Table[
        Random[Integer,Min[rowSums[[i]],colSums[[j]]]], {i,1,
          Length[rowSums]},{j,1,Length[colSums]}];
    mat[[All, 1]]=rowSums- Total /@ Rest /@ mat;
    mat[[1]]=colSums-Plus @@ Rest[mat];
    neg=Position[mat, _?Negative];
    While[Length[neg]>0,
      {a,b}=neg[[1]];neg=Rest[neg];
      Do[If[mat[[a,b]]\[GreaterEqual]0, Return[]];
        If[(d = Min[{mat[[a,j]], mat[[i,b]]}])>0,
          {mat[[a,b]],mat[[a,j]], mat[[i,j]], 
              mat[[i,b]]}={mat[[a,b]],mat[[a,j]], mat[[i,j]], 
                mat[[i,b]]}+{d,-d,d,-d}],
        {i,1, Length[rowSums]}, {j,1,Length[colSums]}]];
    mat]

Because of we start with a random matrix, we cannot predict what the
resulting matrix will be. But, as Bobby already remarked, it is not at
all clear how random the result is.

Fred Simons
Eindhoven University of Technology


  • Prev by Date: Re: simplifying inside sum, Mathematica 5.1
  • Next by Date: Re: simplifying inside sum, Mathematica 5.1
  • Previous by thread: Re: random matrix from row and column sums
  • Next by thread: Calling windows programs