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

Matteo Richiardi matteo.richiardi at gmail.com
Thu Dec 17 01:18:56 CET 2015


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]]



More information about the R-help mailing list