[R] outer on matrices / arrays

From: eariasca <eariasca_at_math.ucsd.edu>
Date: Wed, 16 Jan 2008 12:59:05 -0800


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. Received on Wed 16 Jan 2008 - 21:02:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 Jan 2008 - 21:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive