# populating a large matrix

4 messages
Open this post in threaded view
|
Report Content as Inappropriate

## populating a large matrix

 Hi all, This question is slightly weird.  I am trying to populate a matrix with equations.  The matrix represents transition probabilities between states.  A simple example is:         state1 state2 state3 state1 s d d*d state2 e s d*e state3 e*e e*d s The parameters s, d, and e need to be optimized with an iterative algorithm.  This means I have to modify, say, d, and then recalculate the transition probabilities for each cell. Currently, I do this by making a matrix with the equations in character format, setting s, e, and d to values, and then running each cell through parse(eval(text=celltxt)). As follows: ################################# # Test code: # Make the text matrix txtmat = matrix(c("s", "d", "d*d", "e", "s", "d*e", "e*e", "e*d", "e*d"), nrow=3, byrow=TRUE) s=0.7 d=0.2 e=0.1 doit <- function(celltxt)         {         cellval = eval(parse(text=celltxt))         return(cellval)         } # Calculate the matrix with numerical values matrix_vals = sapply(X=txtmat, FUN=doit) valmat = matrix(matrix_vals, nrow=3, byrow=TRUE) valmat # End test code ################################# ...however, this seems to get slow for large matrices. Since I have to optimize all the parameters I need something that updates the matrix quickly when s, d, or e is changed.   Perhaps this is a job for pointers in C++ or something, but I figure there must be a better way in R. Can anyone think of something more sophisticated than my current method? Thanks in advance for any and all help!! Cheers, Nick -- ==================================================== Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Graduate Student Instructor, IB200B Principles of Phylogenetics: Ecology and Evolution http://ib.berkeley.edu/courses/ib200b/http://phylo.wikidot.com/Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54http://fisher.berkeley.edu/cteg/hlab.htmlDept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.htmlLab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: [hidden email] Mailing address: Department of Integrative Biology 1005 Valley Life Sciences Building #3140 Berkeley, CA 94720-3140 ----------------------------------------------------- "[W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together." Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: populating a large matrix

Open this post in threaded view
|
Report Content as Inappropriate