Hi, 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) then C <- matrix(c(6, 6, 6, 3, 3, 3), byrow=TRUE, ncol=3) Many thanks! Cheers, David
One way to do it: apply(B, 1, function(x) t(apply(A, 1, function(y) abs(y-x) )) ) ----- Original Message ----> From: David Neu <david at davidneu.com> > To: r-help at r-project.org > Sent: Sat, May 22, 2010 10:35:32 AM > Subject: [R] Fast Matrix Computation > > Hi,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 withdimension (m*p,n) in which each row in C is found by applying> somefunction f to each pair of rows (x,y) where x is a row in A and y is> arow 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)then C <- matrix(c(6, 6, 6, 3,> 3, 3), byrow=TRUE, ncol=3)Many> thanks!Cheers, David ______________________________________________> ymailto="mailto:R-help at r-project.org" > href="mailto:R-help at r-project.org">R-help at r-project.org mailing list> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank > >https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting> guide http://www.R-project.org/posting-guide.htmland provide commented,> minimal, self-contained, reproducible code.
Hi David, Here are two suggestions, one that works for the example you provided and a second one for a more general case. # Your example A <- matrix(c(1, 2, 3, 4, 5, 6), byrow=TRUE, ncol=3) B <- matrix(c(7, 8, 9), byrow=TRUE, ncol=3) t(abs(t(A) - as.vector(B))) # More general case B1 <- structure(c(7, 8, 10, 11, 8, 9, 11, 12, 9, 10, 12, 13), .Dim c(4L, 3L)) rA <- 1:nrow(A) rB <- 1:nrow(B1) grid <- as.matrix(expand.grid(rA, rB)) t(apply(grid, 1, function(x) abs(A[x[1], ] - B1[x[2], ]))) # The same using a function foo foo <- function(A, B){ rA <- 1:nrow(A) rB <- 1:nrow(B) grid <- as.matrix(expand.grid(rA, rB)) t(apply(grid, 1, function(x) abs(A[x[1], ] - B1[x[2], ]))) } foo(A, B) foo(A, B1) As usual, there might be better and faster ways to do this. HTH, Jorge On Sat, May 22, 2010 at 1:35 PM, David Neu <> wrote:> Hi, > > 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) > > then > > C <- matrix(c(6, 6, 6, 3, 3, 3), byrow=TRUE, ncol=3) > > Many thanks! > > Cheers, > David > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]