MathGroup Archive 2005

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

Search the Archive

Re: Matrix question

  • To: mathgroup at
  • Subject: [mg60337] Re: Matrix question
  • From: "Dr. Wolfgang Hintze" <weh at>
  • Date: Tue, 13 Sep 2005 06:07:35 -0400 (EDT)
  • References: <dfudth$1qo$>
  • Sender: owner-wri-mathgroup at


understanding that you are looking for a matrix in which a percentage p 
of random positions has the value 1 whereas the rest is 0 (same 
understanding as David Park and Bill Rowe) my solution (very 
straighforward, working without any package and for Version 4) is

Parameters of the problem

n = 10;
p = 0.3;


m = Floor[n^2*p];
A = Array[0 & , {n, n}];

Select a set of m random positions {i,j}

r = {};
While[Length[r] < m, AppendTo[g, {1 + Random[Integer, n - 1], 1 + 
Random[Integer, n - 1]}];
    r = Union[r]]

Assign 1 to the random positions in matrix A

Do[A[[r[[i,1]],r[[i,2]]]] = 1, {i, 1, Length[r]}]



sandog wrote:

> i want to create a NxN matrix with zeros but i want a percentage of the matrix spots to be one at random spots any idea? cause i have no clue how to do that.
> Any help appreciated thx .

  • Prev by Date: Re: Precedence
  • Next by Date: Re: Timed Notebooks for Student Take-home examinations
  • Previous by thread: Re: Matrix question
  • Next by thread: Re: Condition for pure functions