dear all, i have a quite simple question, i want to fill up a Matrix like done in the following function, but the performance is very bad for large dimensions is there a way to do this like with apply or something similar? makeMatrix <- function(a, b,dim) { X=matrix(0,ncol=dim,nrow=dim) for (i in c(1:dim)){ for (j in c(1:dim)) { if (i==j) {X[i,j]<-a} else { X[i,j]<- exp(( -1*abs(i-j))/(3*b)) } } } X } -- View this message in context: http://r.789695.n4.nabble.com/fill-Matrix-quicker-tp3710428p3710428.html Sent from the R help mailing list archive at Nabble.com.
Making use of the row() and col() functions speeds things up a bit. makeMatrix2 <- function(a, b, dim) { X <- matrix(NA, ncol=dim, nrow=dim) X <- exp( (-1*abs(row(X) - col(X)))/(3*b) ) diag(X) <- a X } system.time(makeMatrix(1, 2, 1000)) system.time(makeMatrix2(1, 2, 1000)) Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA From: monk <math@hush.com> To: r-help@r-project.org Date: 08/01/2011 01:20 PM Subject: [R] fill Matrix quicker Sent by: r-help-bounces@r-project.org dear all, i have a quite simple question, i want to fill up a Matrix like done in the following function, but the performance is very bad for large dimensions is there a way to do this like with apply or something similar? makeMatrix <- function(a, b,dim) { X=matrix(0,ncol=dim,nrow=dim) for (i in c(1:dim)){ for (j in c(1:dim)) { if (i==j) {X[i,j]<-a} else { X[i,j]<- exp(( -1*abs(i-j))/(3*b)) } } } X } -- View this message in context: http://r.789695.n4.nabble.com/fill-Matrix-quicker-tp3710428p3710428.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. [[alternative HTML version deleted]]
Most certainly you can speed it up: X <- exp(-abs(row(X) - col(X)) / (3*b)) diag(X) <- a should do what you want. This is called 'vectorization' and is discussed lots of places -- for instance, in the two documents mentioned below in my signature. On 01/08/2011 19:12, monk wrote:> dear all, > > i have a quite simple question, i want to fill up a Matrix like done in the > following function, > but the performance is very bad for large dimensions > is there a way to do this like with apply or something similar? > > > makeMatrix<- function(a, b,dim) { > X=matrix(0,ncol=dim,nrow=dim) > > > > for (i in c(1:dim)){ > for (j in c(1:dim)) { > if (i==j) {X[i,j]<-a} > else { X[i,j]<- exp(( -1*abs(i-j))/(3*b)) } > } > } > X > } > > -- > View this message in context: http://r.789695.n4.nabble.com/fill-Matrix-quicker-tp3710428p3710428.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
thanks a lot , that will do the trick -- View this message in context: http://r.789695.n4.nabble.com/fill-Matrix-quicker-tp3710428p3710533.html Sent from the R help mailing list archive at Nabble.com.
On Aug 1, 2011, at 20:12 , monk wrote:> dear all, > > i have a quite simple question, i want to fill up a Matrix like done in the > following function, > but the performance is very bad for large dimensions > is there a way to do this like with apply or something similar? > > > makeMatrix <- function(a, b,dim) { > X=matrix(0,ncol=dim,nrow=dim) > > > > for (i in c(1:dim)){ > for (j in c(1:dim)) { > if (i==j) {X[i,j]<-a} > else { X[i,j]<- exp(( -1*abs(i-j))/(3*b)) } > } > } > X > } >I'd go for something like X <- outer(1:dim, 1:dim, function(i,j) exp(-abs(i-j)/3/b)) diag(X) <- a> -- > View this message in context: http://r.789695.n4.nabble.com/fill-Matrix-quicker-tp3710428p3710428.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com "D?den skal tape!" --- Nordahl Grieg