Hi, This is a very basic question, but apparently I am too stupid for it. I have a large matrix A, and I need to avoid for loops. How could I apply a function f(a,r,c) on each element of A, using the subscript (row and column) of a as the other arguments? Thanks in advance, Markku Karhunen National Public Health Institute, Finland
r-help-bounces at stat.math.ethz.ch napsal dne 16.04.2007 14:52:55:> Hi, > > This is a very basic question, but apparently I am too stupid for it. > > I have a large matrix A, and I need to avoid for loops. How could I > apply a function f(a,r,c) on each element of A, using the subscript (row> and column) of a as the other arguments?Hi fff<-function(a,b,c) a*b+c x<-1:12 dim(x)<-c(3,4) x [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 fff(x, col(x), row(x)) [,1] [,2] [,3] [,4] [1,] 2 9 22 41 [2,] 4 12 26 46 [3,] 6 15 30 51 works. However from your function description is really tough to understand what the function really does so maybe this is not what you expected. Regards Petr> > Thanks in advance, > Markku Karhunen > National Public Health Institute, > Finland > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
It would be helpful if you could be more specific of what exactly you'd like to compute. Have a look also at the posting guide available at: http://www.R-project.org/posting-guide.html 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: "Markku Karhunen" <markku.karhunen at ktl.fi> To: <r-help at stat.math.ethz.ch> Sent: Monday, April 16, 2007 2:52 PM Subject: [R] Matrix manipulation> Hi, > > This is a very basic question, but apparently I am too stupid for > it. > > I have a large matrix A, and I need to avoid for loops. How could I > apply a function f(a,r,c) on each element of A, using the subscript > (row > and column) of a as the other arguments? > > Thanks in advance, > Markku Karhunen > National Public Health Institute, > Finland > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm