[R] How to efficiently compare each row in a matrix with each row in another matrix?

Hofert Jan Marius marius.hofert at math.ethz.ch
Sat Dec 8 16:28:04 CET 2012


Nice idea, Thomas, thanks. I could further decrease run time a bit, by building the required matrices by hand.

Any other ideas?

Marius <- function(A, B) apply(B, 1, function(b) apply(A, 1, function(a) all(a <= b)))

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.2.0 <- function(A, B){
    nA <- nrow(A)
    nB <- nrow(B)
    C <- do.call(rbind, rep(list(B), nA)) >= matrix(rep(A, each=nB), ncol=ncol(B))
    matrix(rowSums(C) == ncol(A), nA, nB, byrow=TRUE)
}

M <- 5
N <- 1000
P <- 5000
A <- matrix(runif(N,1,1000), nrow=N, ncol=M)
B <- matrix(runif(M,1,1000), nrow=P, ncol=M)

system.time(Marius(A, B))[[3]] # ~ 18s
system.time(foo <- perhaps(A, B))[[3]] # ~ 1.4s
system.time(bar <- Marius.2.0(A, B))[[3]] # ~ 1s
stopifnot(all.equal(foo, bar))




________________________________
From: tgstewart at gmail.com [tgstewart at gmail.com] on behalf of Thomas Stewart [tgs.public.mail at gmail.com]
Sent: Saturday, December 08, 2012 3:46 PM
To: Hofert Jan Marius
Cc: mailman, r-help
Subject: Re: [R] How to efficiently compare each row in a matrix with each row in another matrix?

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.hofert at math.ethz.ch<mailto:marius.hofert at 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 at r-project.org<mailto: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.




More information about the R-help mailing list