MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: random matrix initialisation...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37630] RE: [mg37576] random matrix initialisation...
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 6 Nov 2002 06:59:06 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Manuel Marques-Pita [mailto:manuxu at mac.com]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, November 05, 2002 11:01 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg37630] [mg37576] random matrix initialisation...
>
>
>   
>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
>
>--
>

In[1]:= << DiscreteMath`Permutations`

In[2]:=
X[size_, n_?NonNegative] /; n <= (m = size^2) := 
  Partition[RandomPermutation[m], size] /. i_Integer :> If[i <= n, 1, 0]

In[3]:= X[4, 3]
Out[3]= 
{{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 0}}

In[4]:= X[4, 3]
Out[4]=
{{0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1, 1}}

In[5]:= X[4, 3]
Out[5]=
{{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}

also:

In[7]:= X[4, 0]
Out[7]=
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}

In[8]:= X[4, 16]
Out[8]=
{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}

--
Hartmut Wolf



  • Prev by Date: Re: Re: Not quite a Swell FLOOP?
  • Next by Date: Re: Re: OO in Mathematica
  • Previous by thread: Re: random matrix initialisation...
  • Next by thread: manipulating block diagonal matrices