I'm wanting to perform analysis (e.g. using eigen()) of binary matrices - i.e. matrices comprising 0s and 1s. For example: n<-1000 test.mat<-matrix(round(runif(n^2)),n,n) eigen(test.mat,only.values=T) Is there a more efficient way of setting up test.mat, as each cell only requires a binary digit? I imagine R is setting up a structure which could contain n^2 floats. Thanks in advance for any help. Regards, Mark This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
Mark Edmondson-Jones wrote:> I'm wanting to perform analysis (e.g. using eigen()) of binary matrices - i.e. matrices comprising 0s and 1s. > > For example: > > n<-1000 > test.mat<-matrix(round(runif(n^2)),n,n) > eigen(test.mat,only.values=T) > > Is there a more efficient way of setting up test.mat, as each cell only requires a binary digit? I imagine R is setting up a structure which could contain n^2 floats.No. In principle you could use logicals, but that does not help for further calculations in eigen(). Uwe Ligges> Thanks in advance for any help. > > Regards, > Mark > > > This message has been checked for viruses but the contents of an attachment > may still contain software viruses, which could damage your computer system: > you are advised to perform your own checks. Email communications with the > University of Nottingham may be monitored as permitted by UK legislation. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
you mean something like this: matrix(sample(0:1, n*n, TRUE), n, n) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Mark Edmondson-Jones" <Mark.Edmondson-jones at nottingham.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, April 13, 2005 3:50 PM Subject: [R] Binary Matrices> I'm wanting to perform analysis (e.g. using eigen()) of binary > matrices - i.e. matrices comprising 0s and 1s. > > For example: > > n<-1000 > test.mat<-matrix(round(runif(n^2)),n,n) > eigen(test.mat,only.values=T) > > Is there a more efficient way of setting up test.mat, as each cell > only requires a binary digit? I imagine R is setting up a structure > which could contain n^2 floats. > > Thanks in advance for any help. > > Regards, > Mark > > > This message has been checked for viruses but the contents of an > attachment > may still contain software viruses, which could damage your computer > system: > you are advised to perform your own checks. Email communications > with the > University of Nottingham may be monitored as permitted by UK > legislation. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Actually n^2 doubles. You could insert as.integer() around the call to round(runif()), to make the matrix have storage mode "integer", but when you call "eigen" you need a matrix of doubles anyway, so you're not really saving space. If your matrices are large and have mostly zeros, you might benefit from the SparseM package or the Matrix package. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mark Edmondson-Jones Sent: Wednesday, April 13, 2005 9:51 AM To: r-help at stat.math.ethz.ch Subject: [R] Binary Matrices I'm wanting to perform analysis (e.g. using eigen()) of binary matrices - i.e. matrices comprising 0s and 1s. For example: n<-1000 test.mat<-matrix(round(runif(n^2)),n,n) eigen(test.mat,only.values=T) Is there a more efficient way of setting up test.mat, as each cell only requires a binary digit? I imagine R is setting up a structure which could contain n^2 floats. Thanks in advance for any help. Regards, Mark This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> -----Original Message----- > From: Uwe Ligges > > Mark Edmondson-Jones wrote: > > > I'm wanting to perform analysis (e.g. using eigen()) of > binary matrices - i.e. matrices comprising 0s and 1s. > > > > For example: > > > > n<-1000 > > test.mat<-matrix(round(runif(n^2)),n,n) > > eigen(test.mat,only.values=T) > > > > Is there a more efficient way of setting up test.mat, as > each cell only requires a binary digit? I imagine R is > setting up a structure which could contain n^2 floats. > > No. In principle you could use logicals,... but that doesn't save any memory:> object.size(integer(1e6))[1] 4000028> object.size(logical(1e6))[1] 4000028> but that does not help for further calculations in eigen().Besides, if the problem size is really 1000 x 1000, one matrix in double precision is only 8MB. As Reid said, if the matrix is sparse, there's probably a lot more saving in both memory and computation by using SparseM and Matrix packages. Cheers, Andy> Uwe Ligges > > > > Thanks in advance for any help. > > > > Regards, > > Mark > > > > > > This message has been checked for viruses but the contents > of an attachment > > may still contain software viruses, which could damage your > computer system: > > you are advised to perform your own checks. Email > communications with the > > University of Nottingham may be monitored as permitted by > UK legislation. > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
1000x1000 is only indicative. I need to generate larger (adjacency) matrices using a variety of models. Most are sparse, with a high proportion of zeros and so SparseM sounds very promising. I will investigate. Thanks, Mark>>> "Liaw, Andy" <andy_liaw at merck.com> 13/04/2005 >>>> -----Original Message----- > From: Uwe Ligges > > Mark Edmondson-Jones wrote: > > > I'm wanting to perform analysis (e.g. using eigen()) of > binary matrices - i.e. matrices comprising 0s and 1s. > > > > For example: > > > > n<-1000 > > test.mat<-matrix(round(runif(n^2)),n,n) > > eigen(test.mat,only.values=T) > > > > Is there a more efficient way of setting up test.mat, as > each cell only requires a binary digit? I imagine R is > setting up a structure which could contain n^2 floats. > > No. In principle you could use logicals,... but that doesn't save any memory:> object.size(integer(1e6))[1] 4000028> object.size(logical(1e6))[1] 4000028> but that does not help for further calculations in eigen().Besides, if the problem size is really 1000 x 1000, one matrix in double precision is only 8MB. As Reid said, if the matrix is sparse, there's probably a lot more saving in both memory and computation by using SparseM and Matrix packages. Cheers, Andy> Uwe Ligges > > > > Thanks in advance for any help. > > > > Regards, > > Mark > > > > > > This message has been checked for viruses but the contents > of an attachment > > may still contain software viruses, which could damage your > computer system: > > you are advised to perform your own checks. Email > communications with the > > University of Nottingham may be monitored as permitted by > UK legislation. > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >------------------------------------------------------------------------------ ------------------------------------------------------------------------------ This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.