Re: How to make a loop for this problem!
- To: mathgroup at smc.vnet.net
- Subject: [mg75366] Re: How to make a loop for this problem!
- From: Peter Pein <petsie at dordos.net>
- Date: Fri, 27 Apr 2007 05:19:12 -0400 (EDT)
- References: <f0pkt5$28h$1@smc.vnet.net>
pskmaths at googlemail.com schrieb:
> 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.
>
>
Hi,
there are only 512 distinct 3x3 matrices with elements out of the set {0,1}.
Therefore you can shuffle the list {0,1,2,...,511} and convert each number n
of this list into a matrix. To do this, convert n into binary representation
with leading zeroes and partition this list of 9 digits into three parts:
In[1]:=
<< "DiscreteMath`Combinatorica`"
mats = (Partition[IntegerDigits[#1, 2, 9], 3] & ) /@
RandomPermutation[Range[0, 511]];
the first two matrices:
In[3]:= Take[mats, 2]
Out[3]=
{{{1, 1, 0},
{0, 1, 1},
{1, 1, 0}},
{{1, 1, 0},
{0, 1, 0},
{1, 0, 0}}}
To check for duplicates (impossible):
In[4]:= Sort[mats] === Union[mats]
Out[4]= True
I had to sort "mats" because Union gives a sorted output.
HTH,
Peter