Re: random matrix from row and column sums
- To: mathgroup at smc.vnet.net
- Subject: [mg53581] Re: random matrix from row and column sums
- From: "Astanoff" <astanoff at yahoo.fr>
- Date: Wed, 19 Jan 2005 01:59:32 -0500 (EST)
- References: <csinpt$njk$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
< Is there an efficient method in Mathematica to generate random tables < of nonnegative integers from a list of row sums and a list of column < sums? < For example for row sums of {7,3,2} and column sums of {2,9,1} the < following two tables satisfy the constraints: < {{2, 5, 0}, {0, 2, 1}, {0, 2, 0}} < and < {{1, 6, 0}, {1, 2, 0}, {0, 1, 1}} This is one possible solution : In[1]:= rand[sumLin_List, sumCol_List]:= Module[{n = Length[sumLin], ones,t}, ones=Table[1,{n}]; While[t=Table[Random[Integer,Min[sumLin[[i]],sumCol[[j]]]], {i,1,n}, {j,1,n}]; t[[1]]=sumCol-Plus @@ Rest[t]; ok=And @@ Thread[0 <= t[[1]]] && And @@ Thread[t[[1]] <= sumCol] && t.ones == sumLin && ones.t == sumCol; !ok]; t]; In[2]:= rand[{7,3,2},{2,9,1}] Out[2]= {{1,6,0},{1,1,1},{0,2,0}} In[3]:= rand[{7,3,2,4,5},{2,9,1,5,4}] Out[3]= {{0,3,0,3,1},{0,3,0,0,0},{0,1,0,0,1},{1,2,0,0,1},{1,0,1,2,1}} Not very efficient indeed, but it works for small tables of small numbers. v.a.