Dear Rusers, Trying to reduce my for loops addiction, could somebody tell me if there are ways to simplify (and perhaps accelerate ?) the following line for (i in 1:N) for (j in 1:N) m[i,j] = b[i]-b[j]; (where m is a NxN matrix and b a vector of length N) Thanks for any hint.
vincent at 7d4.com writes:> Dear Rusers, > > Trying to reduce my for loops addiction, > could somebody tell me if there are ways to simplify > (and perhaps accelerate ?) the following line > > for (i in 1:N) for (j in 1:N) m[i,j] = b[i]-b[j]; > > (where m is a NxN matrix and b a vector of length N) > > Thanks for any hint.m <- outer(b, b, "-") -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
vincent at 7d4.com wrote:> Dear Rusers, > > Trying to reduce my for loops addiction, > could somebody tell me if there are ways to simplify > (and perhaps accelerate ?) the following line > > for (i in 1:N) for (j in 1:N) m[i,j] = b[i]-b[j];outer(b, b, "-") Uwe Ligges> (where m is a NxN matrix and b a vector of length N) > > Thanks for any hint. > > ______________________________________________ > 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
try this: m <- outer(b, b, "-") 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/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <vincent at 7d4.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, July 04, 2006 11:43 AM Subject: [R] removing for loop> Dear Rusers, > > Trying to reduce my for loops addiction, > could somebody tell me if there are ways to simplify > (and perhaps accelerate ?) the following line > > for (i in 1:N) for (j in 1:N) m[i,j] = b[i]-b[j]; > > (where m is a NxN matrix and b a vector of length N) > > Thanks for any hint. > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
I guess outer(b,b,"-") gives what you want HIH, Stefano On Tue, Jul 04, 2006 at 11:43:19AM +0200, vincent a 7d4.com wrote: <vincent>Dear Rusers, <vincent> <vincent>Trying to reduce my for loops addiction, <vincent>could somebody tell me if there are ways to simplify <vincent>(and perhaps accelerate ?) the following line <vincent> <vincent>for (i in 1:N) for (j in 1:N) m[i,j] = b[i]-b[j]; <vincent> <vincent>(where m is a NxN matrix and b a vector of length N) <vincent> <vincent>Thanks for any hint. <vincent> <vincent>______________________________________________ <vincent>R-help a stat.math.ethz.ch mailing list <vincent>https://stat.ethz.ch/mailman/listinfo/r-help <vincent>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html