[R] Fast Matrix Computation

David Neu david at davidneu.com
Sat May 22 19:35:32 CEST 2010


I have two (large) matrices A and B of dimensions (m,n) and (p,n) respectively.

I'd like to see if the is a fast way to compute a new matrix C with
dimension (m*p,n) in which each row in C is found by applying some
function f to each pair of rows (x,y) where x is a row in A and y is a
row in B.

For example, if

A <- matrix(c(1, 2, 3, 4, 5, 6), byrow=TRUE, ncol=3)
B <- matrix(c(7, 8, 9), byrow=TRUE, ncol=3)

and f <- function(x,y) abs(y-x)


C <- matrix(c(6, 6, 6, 3, 3, 3), byrow=TRUE, ncol=3)

Many thanks!


More information about the R-help mailing list