Re: random matrix initialisation...
- To: mathgroup at smc.vnet.net
- Subject: [mg37592] Re: random matrix initialisation...
- From: "Lawrence A. Walker Jr." <lwalker701_remove_ at earthlink.net>
- Date: Wed, 6 Nov 2002 06:53:10 -0500 (EST)
- Organization: Morgan State University, COMSARE
- References: <aq86gt$e4j$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi m,
Very interesting question.
The approach is as follows.
First load the Combinatorica package. This package contains the
function, RandomPermutation, that generates a list with the numbers
randomly shuffled.
<<DiscreteMath`Combinatorica`
X[n_, r_]:= Module[{lst},
lst = RandomPermutation[n*n];
lst = lst /. (x_ /; x > r) -> 0;
lst = lst /. (x_ /; x ? 0) -> 1;
Partition[lst, n]];
Then X[4,3] generates the desired matrix.
Hope this helps.
Lawrence
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
>
> --
>
--
Lawrence A. Walker Jr.
http://www.kingshonor.com