Re: random matrix from row and column sums
- To: mathgroup at smc.vnet.net
- Subject: [mg53588] Re: random matrix from row and column sums
- From: DrBob <drbob at bigfoot.com>
- Date: Wed, 19 Jan 2005 01:59:47 -0500 (EST)
- References: <200501181007.FAA23929@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
rowSums = {7, 3, 2, 1};
colSums = {2, 9, 2};
m = Array[a, {Length@rowSums, Length@colSums}];
allSolutions = m /. {ToRules@Reduce[Flatten@{Thread[Tr /@ m == rowSums],
Thread[Tr /@ Transpose@m == colSums], Thread[
Flatten@m >= 0]}, Flatten@m, Integers]};
allSolutions[[Random[Integer,{1,Length@allSolutions}]]]
{{1,6,0},{0,3,0},{1,0,1},{0,0,1}}
(The same code works for your 3x3 example.)
Bobby
On Tue, 18 Jan 2005 05:07:54 -0500 (EST), <adiggle at agric.wa.gov.au> wrote:
> 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
>
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
- References:
- random matrix from row and column sums
- From: adiggle@agric.wa.gov.au
- random matrix from row and column sums