Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-17 Thread David Winsemius
s A&M University > College Station, TX 77840-4352 > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff Newmiller > Sent: Wednesday, December 16, 2015 7:01 PM > To: Matteo Richiardi; r-help@r-project.org > Subject: Re: [R]

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-17 Thread David L Carlson
f Newmiller Sent: Wednesday, December 16, 2015 7:01 PM To: Matteo Richiardi; r-help@r-project.org Subject: Re: [R] Applying a function to a matrix using indexes as arguments Would outer( A, B, `*` ) / C do the trick for you? -- Sent from my phone. Please excuse my brevity. On December 16,

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-17 Thread Matteo Richiardi
Hi David, that's a great answer, thanks so much. I imagined apply() was involved in the solution, but I was unable to find how myself. Thanks again. Matteo On 17 December 2015 at 01:37, David Winsemius wrote: > > > On Dec 16, 2015, at 5:34 PM, David Winsemius > wrote: > > > > > >> On Dec 16, 20

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread David Winsemius
> On Dec 16, 2015, at 5:34 PM, David Winsemius wrote: > > >> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi >> 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.

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread David Winsemius
> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi > 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<-

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread Jeff Newmiller
Would outer( A, B, `*` ) / C do the trick for you? -- Sent from my phone. Please excuse my brevity. On December 16, 2015 4:41:13 PM PST, Matteo Richiardi wrote: >My problem is of course more complicated, and is obviously not a >homework. >I just wanted to provide a minimal working example. Y

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread Matteo Richiardi
My problem is of course more complicated, and is obviously not a homework. I just wanted to provide a minimal working example. You can replace the matrix C with a matrix containing any number, for what matters. Btw, because numbers are extracted from a Gaussian distribution, the likelihood that you

Re: [R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread Jeff Newmiller
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

[R] Applying a function to a matrix using indexes as arguments

2015-12-16 Thread Matteo Richiardi
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