Re: How to make a loop for this problem!

• To: mathgroup at smc.vnet.net
• Subject: [mg75399] Re: How to make a loop for this problem!
• From: Ray Koopman <koopman at sfu.ca>
• Date: Sat, 28 Apr 2007 05:58:01 -0400 (EDT)
• References: <f0pkt5\$28h\$1@smc.vnet.net><f0sfck\$n5q\$1@smc.vnet.net>

```On Apr 27, 2:22 am, Ray Koopman <koop... at sfu.ca> wrote:
> On Apr 26, 12:38 am, pskma... at googlemail.com wrote:
>>
>> Hi all,
>>
>> This comand:
>> A = Array[Random[Integer] &, {3, 3}]
>> generates a 3x3 random matrix its terms between 0 and 1.
>>
>> I need to make a loop that geerates a finite number of matrices, let's
>> say 512 and this loop check that non of the matrices is equal to
>> another one (no repeated matrices)
>>
>> I can geerate thoses random, matrices with this command:
>> Do[Print[Array[Random[Integer] &, {3, 3}]], {512}]
>> but I may have two matrices that are equal and also as far as I know I
>> cann't use the out put because of the command, Print.
>
> This will generate m different random n x n binary matrices, but
> it will work only for small n's because it takes a simple-minded
> approach to selecting m different random integers in Range[2^n^2].
>
> gen[m_Integer?Positive, n_Integer?Positive] /; m <= 2^n^2 :=
> Partition[#,n]& /@ IntegerDigits[ Ordering[
>                   Table[Random[],{2^n^2}], m] - 1, 2, n^2]
>
> gen[16,2]
>
> {{{0,1},{0,0}}, {{0,0},{0,0}}, {{1,0},{1,0}}, {{1,1},{1,1}},
>  {{0,0},{1,0}}, {{1,1},{0,1}}, {{0,1},{1,0}}, {{0,0},{0,1}},
>  {{1,1},{1,0}}, {{0,1},{1,1}}, {{1,0},{0,0}}, {{0,1},{0,1}},
>  {{1,0},{1,1}}, {{0,0},{1,1}}, {{1,1},{0,0}}, {{1,0},{0,1}}}
>
> gen[16,5]
>
> {{{1,0,1,0,1},{1,0,1,1,0},{1,0,0,1,1},{0,1,1,0,0},{0,0,1,0,0}},
>  {{1,0,1,1,0},{1,0,0,1,0},{0,0,1,0,1},{1,0,1,0,0},{1,1,1,1,1}},
>  {{1,1,1,0,0},{0,0,0,1,1},{0,0,1,1,1},{1,1,1,0,1},{0,1,1,0,0}},
>  {{1,0,0,1,1},{1,1,1,0,0},{1,0,0,1,1},{1,0,0,1,1},{1,0,1,1,0}},
>  {{0,1,1,1,0},{1,1,1,0,0},{1,0,1,1,0},{0,0,0,0,0},{1,0,0,0,0}},
>  {{0,0,1,0,1},{1,0,1,0,0},{0,1,1,0,1},{0,0,1,0,0},{0,1,0,1,1}},
>  {{0,1,0,0,1},{0,1,1,1,0},{1,1,1,0,0},{0,1,1,1,0},{0,1,1,0,1}},
>  {{1,0,0,0,0},{0,1,1,1,0},{0,1,1,1,0},{0,1,0,0,0},{0,0,1,1,1}},
>  {{1,0,0,0,1},{1,1,0,0,1},{0,1,0,1,0},{0,1,1,0,1},{0,0,1,0,0}},
>  {{0,0,0,0,1},{0,0,1,1,1},{0,1,0,1,0},{0,0,1,0,1},{1,0,0,1,1}},
>  {{1,0,1,1,1},{0,1,1,0,1},{1,1,1,1,1},{0,0,1,0,0},{0,1,0,1,0}},
>  {{0,1,1,0,0},{1,1,0,0,1},{0,0,1,1,1},{0,0,0,0,1},{1,1,0,1,0}},
>  {{0,1,0,0,0},{0,1,0,1,0},{1,0,1,0,0},{0,1,0,0,1},{0,1,0,1,1}},
>  {{0,0,0,0,0},{1,1,1,0,0},{1,0,1,1,1},{0,0,0,0,1},{0,1,0,0,1}},
>  {{0,0,1,0,0},{1,0,1,0,0},{0,0,1,1,0},{1,1,0,0,0},{1,1,1,1,0}},
>  {{1,0,0,1,1},{1,0,0,0,0},{0,0,0,1,0},{1,1,0,0,0},{0,1,0,0,1}}}

This works for large n but is very inefficient if m is close
to 2^n^2. If you plan on generating these matrices routinely,
you may want to write a routine that combines this with one
of the small-n routines that have been suggested, along with
a decision rule for switching between the two approaches.

genn[m_Integer?Positive, n_Integer?Positive] /; m <= 2^n^2 :=
Block[{t = {Table[Random[Integer],{n^2}]}, u},
Do[While[MemberQ[t, u = Table[Random[Integer],{n^2}]]];
AppendTo[t,u], {m-1}];
Partition[#,n]& /@ t]

genn[16,5]

{{{1,0,1,1,0},{0,1,0,1,1},{1,0,1,1,1},{1,0,1,0,1},{0,1,0,1,1}},
{{1,0,1,0,0},{1,1,0,1,0},{1,0,0,1,0},{0,1,1,0,1},{1,0,0,1,0}},
{{1,1,1,1,1},{1,1,0,0,0},{0,0,0,0,0},{1,1,0,1,0},{0,0,1,0,1}},
{{0,0,1,1,1},{0,0,1,0,1},{0,0,0,1,0},{1,0,0,0,1},{1,0,0,0,0}},
{{0,1,1,0,1},{1,0,1,1,1},{0,0,1,0,1},{1,0,0,1,1},{0,1,1,0,1}},
{{1,1,0,1,0},{0,0,1,0,1},{1,1,0,0,0},{0,1,1,0,1},{1,0,1,1,1}},
{{0,0,0,1,0},{0,0,1,0,0},{0,0,1,0,1},{0,0,1,1,0},{1,1,1,0,0}},
{{0,1,0,0,0},{1,1,0,1,0},{1,0,0,1,0},{1,0,1,0,1},{0,1,0,1,0}},
{{0,0,1,0,0},{0,0,1,1,1},{1,1,0,1,1},{1,1,1,1,0},{1,1,1,1,0}},
{{0,0,0,0,0},{0,1,0,1,1},{1,0,1,0,1},{0,0,1,0,0},{0,1,1,1,0}},
{{1,0,1,1,1},{0,1,1,0,0},{0,1,0,1,1},{0,1,0,1,1},{1,1,1,1,1}},
{{1,0,1,1,1},{0,1,0,1,1},{0,1,0,1,1},{0,1,1,0,0},{0,1,1,0,0}},
{{1,0,0,0,1},{0,0,0,0,0},{0,0,0,0,1},{1,1,1,1,1},{0,1,0,0,0}},
{{0,0,0,0,1},{1,0,1,0,1},{0,0,0,1,1},{1,0,0,0,0},{1,0,0,0,1}},
{{0,1,1,1,0},{1,0,1,0,0},{1,1,1,1,0},{1,1,1,0,1},{1,1,0,0,0}},
{{1,0,1,1,0},{0,0,1,1,0},{0,0,0,1,1},{0,0,0,0,1},{1,0,1,1,0}}}

```

• Prev by Date: Re: remote kernel
• Next by Date: Re: Solving a differential equation numerically in Mathematica
• Previous by thread: Re: How to make a loop for this problem!
• Next by thread: MeijerG function