[R] filling a matrix who's entries are a function of the indices?

Douglas G. Scofield d.scofield at umiami.edu
Sat Aug 23 22:39:27 CEST 2003


Dear R list,

What's the best way in R to fill a matrix who's entries depend on some
function of the indices?  I'm currently doing:

   Q <- matrix(0, k, k)
   for (A in 1:k) {
      for (B in 1:k) {
         Q[A,B] <- my.function(A,B)
      }
   }

but I wonder if there is a more terse way.

Regards,

Douglas Scofield                 Department of Biology 
d.scofield at umiami.edu            University of Miami
off: (305) 284-3778              P.O. Box 249118
fax: (305) 284-3039              Coral Gables, FL  33124-0421




More information about the R-help mailing list