MathGroup Archive 2005

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

Search the Archive

Re: Matrix question


randommatrix[n_, p_] :=
  Module[{worklist, numones = Floor[p n^2]},
    worklist = Join[Array[1 &, numones], Array[0 &, n^2 - numones]];
    worklist = Part[worklist, Ordering[RandomPermutation[n^2]]];
    Partition[worklist, n]]

randommatrix[4, 0.5] // MatrixForm

First I made an array of the required number of 1's join to the remainder
zeros. Then I made a random permutation of the list. Then I partitioned the
list into rows.

David Park
djmp at

From: sandog [mailto:sandog at]
To: mathgroup at

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: Generate a palette with a FrontEnd Token
  • Next by Date: Re: Switching to Mac caused 20x slowdown - please help!
  • Previous by thread: Re: Matrix question
  • Next by thread: Re: Matrix question