Skip to content
Prev 276369 / 398506 Next

Counting number of common elements between the rows of two different matrices

Try this:

# create dummy data
a <- matrix(sample(20, 50, TRUE), ncol = 5)
b <- matrix(sample(20, 50, TRUE), ncol = 5)
# create combinations to test
x <- expand.grid(seq(nrow(a)), seq(nrow(b)))
# test
result <- mapply(function(m1, m2) any(a[m1, ] %in% b[m2, ])
            , x[, 1]
            , x[, 2]
            )
# create the output matrix
result.m <- matrix(result, nrow = nrow(a), ncol = nrow(b))
On Fri, Nov 4, 2011 at 8:51 AM, Parodi, Pietro <Pietro.Parodi at willis.com> wrote: