[R] fill binary matrices with random 1s

2011-11-29 Thread Grant McDonald

Dear all, I am finding difficulty in the following, I would like to 
create an empty matrix e.g. 10x10 of 0s and sequentially fill this 
matrix with randomly placed a 1s until it is saturated. Producing 100 
matrices of sequentially increasing density., This process needs to be 
randomized 1000 times., I assume i should run this along the following 
lines, 1) Create 1000 matrices all zeros, 2) add a random 1 to all 
matrices, 3) run function on all 1000 matrices and output results to a 
vector table (i.e. calculate density of matric at each step for all 100 
matrices.
)., 4) add another 1 to the previous 1000 matrices in a 
random position., repeat till all matrices saturated., I have looked 
through histories on random fill algorithms but all packages I can find 
nothing as simple as the random fill I am looking for., sorry for 
bothering, Thank you for any help in advance.


Something that starts along the lines of the following? Sorry this example is 
atrocious.

matrixfill - function(emptymatrix, K=fullmatrix, time=100, from=0, to=time)

{

N - numeric(time+1)

N[1] - emptymatrix

for (i in 1:time) N[i+1] - N[i]+place random 1 in a random xy position until 
K.
Calculate Density of matrix

  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fill binary matrices with random 1s

2011-11-29 Thread Sarah Goslee
I have to admit I'm not entirely sure what your question is. How to
put a 1 in a random position in a matrix?

mat - matrix(0, 10, 10)
 mat[sample(1:nrow(mat), 1), sample(1:ncol(mat), 1)] - 1
will do so, but if you need to fill a random position that is *currently zero*
then you'll need to wrap it in a while loop and check the value of that cell.

Or, more elegantly, create a random vector of positions in advance,
then fill each:
tofill - sample(1:100)
for(i in 1:length(tofill)) {
mat[tofill[i]] - 1
}

But if you don't need sequential matrices, just random matrices of
particular densities, there are nicer ways to create them.

matdensity - 45
matsize - 10
mat45 - matrix(sample(c(rep(1, matdensity), rep(0, matsize*2 -
matdensity))), matsize, matsize)


On Tue, Nov 29, 2011 at 7:32 AM, Grant McDonald
grantforacco...@hotmail.co.uk wrote:

 Dear all, I am finding difficulty in the following, I would like to
 create an empty matrix e.g. 10x10 of 0s and sequentially fill this
 matrix with randomly placed a 1s until it is saturated. Producing 100
 matrices of sequentially increasing density., This process needs to be
 randomized 1000 times., I assume i should run this along the following
 lines, 1) Create 1000 matrices all zeros, 2) add a random 1 to all
 matrices, 3) run function on all 1000 matrices and output results to a
 vector table (i.e. calculate density of matric at each step for all 100 
 matrices.
 )., 4) add another 1 to the previous 1000 matrices in a
 random position., repeat till all matrices saturated., I have looked
 through histories on random fill algorithms but all packages I can find
 nothing as simple as the random fill I am looking for., sorry for
 bothering, Thank you for any help in advance.


 Something that starts along the lines of the following? Sorry this example is 
 atrocious.

 matrixfill - function(emptymatrix, K=fullmatrix, time=100, from=0, to=time)

 {

 N - numeric(time+1)

 N[1] - emptymatrix

 for (i in 1:time) N[i+1] - N[i]+place random 1 in a random xy position 
 until K.
 Calculate Density of matrix



-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fill binary matrices with random 1s

2011-11-29 Thread Bert Gunter
Folks:

On Tue, Nov 29, 2011 at 6:24 AM, Sarah Goslee sarah.gos...@gmail.com wrote:
 I have to admit I'm not entirely sure what your question is. How to
 put a 1 in a random position in a matrix?

 mat - matrix(0, 10, 10)
  mat[sample(1:nrow(mat), 1), sample(1:ncol(mat), 1)] - 1
This is unnecessary. In R: matrices are simply vectors with a dim
attribute, so you can treat them as vectors:

mat[sample(nrow(mat)*ncol(mat),1] - 1

Moreover, this also suggests a simple way to do this sequentially:
Simply create your vector of random indices at one go and use it for
your loop -- no checking on what previously was sampled is necessary:

ransamp - sample(100,100)  ## assuming nrow = ncol = 10

for( i in 1:100) {
  mat[ransamp[i]] - 1
## do whatever you want
}

HTH

Cheers,
Bert

 will do so, but if you need to fill a random position that is *currently zero*
 then you'll need to wrap it in a while loop and check the value of that cell.

 Or, more elegantly, create a random vector of positions in advance,
 then fill each:
 tofill - sample(1:100)
 for(i in 1:length(tofill)) {
 mat[tofill[i]] - 1
 }

 But if you don't need sequential matrices, just random matrices of
 particular densities, there are nicer ways to create them.

 matdensity - 45
 matsize - 10
 mat45 - matrix(sample(c(rep(1, matdensity), rep(0, matsize*2 -
 matdensity))), matsize, matsize)


 On Tue, Nov 29, 2011 at 7:32 AM, Grant McDonald
 grantforacco...@hotmail.co.uk wrote:

 Dear all, I am finding difficulty in the following, I would like to
 create an empty matrix e.g. 10x10 of 0s and sequentially fill this
 matrix with randomly placed a 1s until it is saturated. Producing 100
 matrices of sequentially increasing density., This process needs to be
 randomized 1000 times., I assume i should run this along the following
 lines, 1) Create 1000 matrices all zeros, 2) add a random 1 to all
 matrices, 3) run function on all 1000 matrices and output results to a
 vector table (i.e. calculate density of matric at each step for all 100 
 matrices.
 )., 4) add another 1 to the previous 1000 matrices in a
 random position., repeat till all matrices saturated., I have looked
 through histories on random fill algorithms but all packages I can find
 nothing as simple as the random fill I am looking for., sorry for
 bothering, Thank you for any help in advance.


 Something that starts along the lines of the following? Sorry this example 
 is atrocious.

 matrixfill - function(emptymatrix, K=fullmatrix, time=100, from=0, to=time)

 {

 N - numeric(time+1)

 N[1] - emptymatrix

 for (i in 1:time) N[i+1] - N[i]+place random 1 in a random xy position 
 until K.
 Calculate Density of matrix



 --
 Sarah Goslee
 http://www.functionaldiversity.org

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fill binary matrices with random 1s

2011-11-29 Thread David Winsemius


On Nov 29, 2011, at 7:32 AM, Grant McDonald wrote:



Dear all, I am finding difficulty in the following, I would like to
create an empty matrix e.g. 10x10 of 0s and sequentially fill this
matrix with randomly placed a 1s until it is saturated. Producing 100
matrices of sequentially increasing density., This process needs to be
randomized 1000 times., I assume i should run this along the following
lines, 1) Create 1000 matrices all zeros, 2) add a random 1 to all
matrices, 3) run function on all 1000 matrices and output results to a
vector table (i.e. calculate density of matric at each step for all  
100 matrices.

)., 4) add another 1 to the previous 1000 matrices in a
random position., repeat till all matrices saturated., I have looked
through histories on random fill algorithms but all packages I can  
find

nothing as simple as the random fill I am looking for., sorry for
bothering, Thank you for any help in advance.


Something that starts along the lines of the following? Sorry this  
example is atrocious.


matrixfill - function(emptymatrix, K=fullmatrix, time=100, from=0,  
to=time)


{

N - numeric(time+1)

N[1] - emptymatrix

for (i in 1:time) N[i+1] - N[i]+place random 1 in a random xy  
position until K.

Calculate Density of matrix


Ewww. That looks painful. Consider this as an alternative to create a  
single such random matrix:


rmat - matrix(rbinom(100, 1, prob=0.1), 10,10)

 rmat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]000000001 0
 [2,]100000000 0
 [3,]000010000 0
 [4,]100000000 0
 [5,]000110000 0
 [6,]000000100 0
 [7,]000000001 0
 [8,]010000000 0
 [9,]000000001 0
[10,]000001000 0


--

David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fill binary matrices with random 1s

2011-11-29 Thread Dennis Murphy
Hi:

Here's one approach. I assume that your first 1000 matrices have a
single 1 in each matrix, the next set of 1000 have two 1's, ..., and
the last one has 99 1's. (No point in doing all 1's since they're all
constant.) If that's the case, then try the following.

# Each row represents a different 'density' of 1's
# upper triangle of m is 0
m - matrix(0, 100, 100)
m[lower.tri(m)] - 1
diag(m) - 1
m - m[-100, ]   # remove row of all 1's

# Functions to operate on a single matrix 
# Function to permute a vector of 0's and 1's
# and reshape it into a 10 x 10 matrix
randomMatrix - function(x) matrix(sample(x), 10, 10)

# Generate a 10 x 10 x 1000 array
marray - function(x) replicate(1000, randomMatrix(x))

# Create a vector of names to which to assign the results
# of simulating from each row of m:
arraynames - paste('array', 1:99, sep = '')

# apply the marray() function to each row of m and assign
# to the corresponding index of arraynames
for(i in seq_along(arraynames)) assign(arraynames[i], marray(m[i, ]))

HTH,
Dennis


On Tue, Nov 29, 2011 at 4:32 AM, Grant McDonald
grantforacco...@hotmail.co.uk wrote:

 Dear all, I am finding difficulty in the following, I would like to
 create an empty matrix e.g. 10x10 of 0s and sequentially fill this
 matrix with randomly placed a 1s until it is saturated. Producing 100
 matrices of sequentially increasing density., This process needs to be
 randomized 1000 times., I assume i should run this along the following
 lines, 1) Create 1000 matrices all zeros, 2) add a random 1 to all
 matrices, 3) run function on all 1000 matrices and output results to a
 vector table (i.e. calculate density of matric at each step for all 100 
 matrices.
 )., 4) add another 1 to the previous 1000 matrices in a
 random position., repeat till all matrices saturated., I have looked
 through histories on random fill algorithms but all packages I can find
 nothing as simple as the random fill I am looking for., sorry for
 bothering, Thank you for any help in advance.


 Something that starts along the lines of the following? Sorry this example is 
 atrocious.

 matrixfill - function(emptymatrix, K=fullmatrix, time=100, from=0, to=time)

 {

 N - numeric(time+1)

 N[1] - emptymatrix

 for (i in 1:time) N[i+1] - N[i]+place random 1 in a random xy position 
 until K.
 Calculate Density of matrix

 
        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.