Re: random matrix initialisation...
- To: mathgroup at smc.vnet.net
- Subject: [mg37613] Re: random matrix initialisation...
- From: Mark Westwood <MarkCWestwood at compuserve.com>
- Date: Wed, 6 Nov 2002 06:54:52 -0500 (EST)
- References: <aq86gt$e4j$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Manuel This is my first go, for an m x m matrix, with n 1s i) load Combinatorica, we'll need it in a step or two: << DiscreteMath`Combinatorica` ii) create a list with n 1s followed by (m * m)- n 0s: list = Join[Table[1, {n}], Table[0, {m m - n}] iii) Create a random permutation of this list, using the Combinatorica function RandomPermutation: randomList = RandomPermutation[list] iv) now partition the list into m rows each of m elements: randomMatrix = Partition[randomList, m] v) putting it all together: x[m_, n_] := Partition[RandomPermutation[Join[Table[1, {n}], Table[0, {m m - n}]]], m] I think this answers your question. Regards Mark Westwood Manuel Marques-Pita wrote: > > > hello, > > i have a question regarding matrices. searched on the web and forums > but maybe due to using wrong keywords i am not finding an answer... > > i need a way or function X to initialise matrices in mathematica such > that each time i call X i get a matrix with a predetermined number of > elements initialised as "1" and the rest as "0" > > examples > > X(4,3) > > {{0,0,0,0}, {0,0,1,0}, {0,0,0,1}, {1,0,0,0}} > > again X(4,3) > > {{1,1,0,0}, {0,0,0,0}, {0,0,0,0}, {0,1,0,0}} > > etc.. (first argument means that the matrix is 4x4 and second means > that there should be 3 "1's" present in the matrix > > any help, pointers, etc will be appreciated! > > thanks a lot > > m > > --