Check out:
https://stat.ethz.ch/pipermail/r-help/2005-January/065085.html
On Jan 16, 2008 3:59 PM, eariasca <eariasca at math.ucsd.edu>
wrote:> Hi,
>
> I would like to evaluate a function f(x,y) taking two vectors as
> entries on all pairs of X rows and Y rows, where X and Y are n-by-p
> and m-by-p matrices respectively. Of course, I would like to avoid
> loops if possible, as the dataset I want to process is quite large.
> I was not able to find a combination of outer() and apply() to do
> this -- note that I am fairly new to R. Below is a simple example of
> what I would like to do -- for illustration purposes only, as I am
> not interested in this particular function.
>
> X = matrix(sample(1:3,size=15,replace=TRUE),5,3)
> Y = matrix(sample(1:3,size=10,replace=TRUE),5,3)
>
> f = function(x,y) {
> max(x/y)
> }
>
> A = matrix(0,5,5)
>
> for (i in 1:5) {
> for (j in 1:5) {
> A[i,j] = f(X[i,],Y[j,])
> }
> }
>
> Thanks for your help.
>
> Ery Arias-Castro
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>