fill binary matrices with random 1s

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

fill binary matrices with random 1s

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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: fill binary matrices with random 1s

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
<[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: fill binary matrices with random 1s

Bert Gunter
Folks:

On Tue, Nov 29, 2011 at 6:24 AM, Sarah Goslee <[hidden email]> 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
> <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: fill binary matrices with random 1s

David Winsemius
In reply to this post by Grant McDonald

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,]    0    0    0    0    0    0    0    0    1     0
  [2,]    1    0    0    0    0    0    0    0    0     0
  [3,]    0    0    0    0    1    0    0    0    0     0
  [4,]    1    0    0    0    0    0    0    0    0     0
  [5,]    0    0    0    1    1    0    0    0    0     0
  [6,]    0    0    0    0    0    0    1    0    0     0
  [7,]    0    0    0    0    0    0    0    0    1     0
  [8,]    0    1    0    0    0    0    0    0    0     0
  [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    1    0    0    0     0


--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: fill binary matrices with random 1s

djmuseR
In reply to this post by Grant McDonald
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
<[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.