Matteo Richiardi
2015-Dec-17 00:18 UTC
[R] Applying a function to a matrix using indexes as arguments
I have to evolve each element of a matrix W W <- matrix(0,2,3) according to some function which uses the indices of the matrix [i,j] as arguments: w.fun = function(i,j) { return A[i]*B[j]/(C[i,j]) } where A<-c(100,100) B<-c(200,200,200) C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) How can I do it, without recurring to a loop? Also, in my application I need to pass the function another argument. Thanks a lot for your suggestions. Matteo [[alternative HTML version deleted]]
Jeff Newmiller
2015-Dec-17 00:36 UTC
[R] Applying a function to a matrix using indexes as arguments
This calculation divides by values centered around zero. The only context that I can think of that would require such silliness is a homework problem, and this list has a no-homework policy. If not, then mentioning the theory you are applying might help someone point you at an existing function that achieves your goals while avoiding divide-by-zero errors. Since you also posted in HTML I gather that you have not read the Posting Guide mentioned below. Avoiding HTML on this list is to your benefit, since using it inevitably leads to others seeing a garbled version of what you sent. Please read the PG for more important guidance. -- Sent from my phone. Please excuse my brevity. On December 16, 2015 4:18:56 PM PST, Matteo Richiardi <matteo.richiardi at gmail.com> wrote:>I have to evolve each element of a matrix W > >W <- matrix(0,2,3) > >according to some function which uses the indices of the matrix [i,j] >as >arguments: >w.fun = function(i,j) { > return A[i]*B[j]/(C[i,j]) >} > >where >A<-c(100,100) >B<-c(200,200,200) >C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) > >How can I do it, without recurring to a loop? Also, in my application I >need to pass the function another argument. > >Thanks a lot for your suggestions. >Matteo > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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]]
David Winsemius
2015-Dec-17 01:34 UTC
[R] Applying a function to a matrix using indexes as arguments
> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi <matteo.richiardi at gmail.com> wrote: > > I have to evolve each element of a matrix W > > W <- matrix(0,2,3) > > according to some function which uses the indices of the matrix [i,j] as > arguments: > w.fun = function(i,j) { > return A[i]*B[j]/(C[i,j]) > } > > where > A<-c(100,100) > B<-c(200,200,200) > C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) > > How can I do it, without recurring to a loop? Also, in my application I > need to pass the function another argument.mapply( function( i,j,fac) {fac*A[i]*B[j]/C[i,j]}, i=row(W), j=col(W), MoreArgs=list(fac=10) ) [1] -86207.97 325768.16 -135764.41 -913036.95 -142509.39 [6] 243715.67 N.B. all of the *apply functions are really loops. -- David.> > Thanks a lot for your suggestions. > Matteo > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA
David Winsemius
2015-Dec-17 01:37 UTC
[R] Applying a function to a matrix using indexes as arguments
> On Dec 16, 2015, at 5:34 PM, David Winsemius <dwinsemius at comcast.net> wrote: > > >> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi <matteo.richiardi at gmail.com> wrote: >> >> I have to evolve each element of a matrix W >> >> W <- matrix(0,2,3) >> >> according to some function which uses the indices of the matrix [i,j] as >> arguments: >> w.fun = function(i,j) { >> return A[i]*B[j]/(C[i,j]) >> } >> >> where >> A<-c(100,100) >> B<-c(200,200,200) >> C <- matrix( rnorm(6,mean=0,sd=1), 2, 3) >> >> How can I do it, without recurring to a loop? Also, in my application I >> need to pass the function another argument. > > mapply( function( i,j,fac) {fac*A[i]*B[j]/C[i,j]}, > i=row(W), > j=col(W), > MoreArgs=list(fac=10) ) > [1] -86207.97 325768.16 -135764.41 -913036.95 -142509.39 > [6] 243715.67 > > > N.B. all of the *apply functions are really loops.Furthermore, you can neatly populate the W matrix with the `[<-` function: W[] <- mapply( function( i,j,fac) {fac*A[i]*B[j]/C[i,j]}, i=row(W), j=col(W),MoreArgs=list(fac=10) )> W[,1] [,2] [,3] [1,] -86207.97 -135764.4 -142509.4 [2,] 325768.16 -913037.0 243715.7>> -- > David. >> >> Thanks a lot for your suggestions. >> Matteo >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > David Winsemius > Alameda, CA, USA > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA