One option is to consider a Kronecker-type expansion. See code below. -tgs
perhaps <- function(A,B){ nA <- nrow(A) nB <- nrow(B) C <- kronecker(matrix(1,nrow=nA,ncol=1),B) >= kronecker(A,matrix(1,nrow=nB,ncol=1)) matrix(rowSums(C) == ncol(A), nA, nB, byrow=TRUE) } Marius <- function(A,B) apply(B, 1, function(b) apply(A, 1, function(a) all(a <= b))) N <- 1000 M <- 5 P <- 5000 A <- matrix(runif(N,1,1000),nrow=N,ncol=M) B <- matrix(runif(M,1,1000),nrow=P,ncol=M) system.time(perhaps(A,B)) system.time(Marius(A,B)) On Sat, Dec 8, 2012 at 6:28 AM, Marius Hofert <marius.hof...@math.ethz.ch>wrote: > Dear expeRts, > > I have two matrices A and B. They have the same number of columns but > possibly different number of rows. I would like to compare each row of A > with each row of B and check whether all entries in a row of A are less > than or equal to all entries in a row of B. Here is a minimal working > example: > > A <- rbind(matrix(1:4, ncol=2, byrow=TRUE), c(6, 2)) # (3, 2) matrix > B <- matrix(1:10, ncol=2) # (5, 2) matrix > ( ind <- apply(B, 1, function(b) apply(A, 1, function(a) all(a <= b))) ) # > (3, 5) = (nrow(A), nrow(B)) matrix > > The question is: How can this be implemented more efficiently in R, that > is, in a faster way? > > Thanks & cheers, > > Marius > > ______________________________________________ > 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]] ______________________________________________ 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.