Re: How to make a loop for this problem!
- To: mathgroup at smc.vnet.net
- Subject: [mg75376] Re: How to make a loop for this problem!
- From: Albert <awnl at arcor.net>
- Date: Fri, 27 Apr 2007 05:24:29 -0400 (EDT)
- References: <f0pkt5$28h$1@smc.vnet.net>
pskmaths 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. if you use Random[Integer] then the entries will be either 0 or 1, not anything in between. There are only 512 possibilities (9^3) of 3x3 Matrices containing 0 or 1 only. So it does not make much sense to generate them this way... > 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. to generate 512 unique matrices with entries between 0 and 1 you could use: matrixlist = Take[ Union[ Table[Array[Random[]&,{3,3}],{600}] ], 512 ]; you can then access the 12th matrix with: matrixlist[[12]] The code above generates some extra matrices, then uses Union to delete doubles and takes the first 512 unique matrices. Of course it could happen that the 600-512 extra matrices are not enough, but I think that is very very unlikely and easy to solve (in fact I think it almost never happens that you will get a double at all!). Also note that the matrices are sorted in some way when using Union. If that is a problem you can use UnsortedUnion which is defined in the further examples section for the documentation of Union... hth, albert