MathGroup Archive 2005

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

Search the Archive

Re: random matrix from row and column sums


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


  • Prev by Date: Re: Calling windows programs
  • Next by Date: Re: random matrix from row and column sums
  • Previous by thread: random matrix from row and column sums
  • Next by thread: Re: random matrix from row and column sums