Re: random matrix from row and column sums
- To: mathgroup at smc.vnet.net
- Subject: [mg53615] Re: random matrix from row and column sums
- From: koopman at sfu.ca (Ray Koopman)
- Date: Thu, 20 Jan 2005 03:47:59 -0500 (EST)
- References: <csinpt$njk$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
adiggle at agric.wa.gov.au wrote in message news:<csinpt$njk$1 at smc.vnet.net>...
> 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}}
>
> Thanks
In[1]:=
kx = Length[fx = {7,3,2}]; cfx = FoldList[Plus,0,fx]
Out[1]=
{0,7,10,12}
In[2]:=
ky = Length[fy = {2,9,1}]; y = Flatten@MapIndexed[Table[#2,{#1}]&,fy]
Out[2]=
{1,1,2,2,2,2,2,2,2,2,2,3}
In[3]:=
fxy = First /@ Split@Sort[
Function[z,Table[Table[Count[#,iy],{iy,ky}]&
[Take[z,{cfx[[ix]]+1,cfx[[ix+1]]}]],{ix,kx}]] /@ Permutations@y]
Out[3]=
{{{0,6,1},{0,3,0},{2,0,0}},
{{0,6,1},{1,2,0},{1,1,0}},
{{0,6,1},{2,1,0},{0,2,0}},
{{0,7,0},{0,2,1},{2,0,0}},
{{0,7,0},{1,1,1},{1,1,0}},
{{0,7,0},{1,2,0},{1,0,1}},
{{0,7,0},{2,0,1},{0,2,0}},
{{0,7,0},{2,1,0},{0,1,1}},
{{1,5,1},{0,3,0},{1,1,0}},
{{1,5,1},{1,2,0},{0,2,0}},
{{1,6,0},{0,2,1},{1,1,0}},
{{1,6,0},{0,3,0},{1,0,1}},
{{1,6,0},{1,1,1},{0,2,0}},
{{1,6,0},{1,2,0},{0,1,1}},
{{2,4,1},{0,3,0},{0,2,0}},
{{2,5,0},{0,2,1},{0,2,0}},
{{2,5,0},{0,3,0},{0,1,1}}}