# [R] Counting number of common elements between the rows of two different matrices

jim holtman jholtman at gmail.com
Fri Nov 4 14:37:50 CET 2011

```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:
>
> Hello
>
> I'm trying to solve this problem without using a for loop but I have so
> far failed to find a solution.
>
> I have two matrices of K columns each, e.g. (K=5), and with numbers of
> row N_A and N_B respectively
>
> A =     (1 5 3 8 15;
>         2 7 20 11 13;
>         12 19 20 21 43)
>
> B =     (2 6 30 8 16;
>         3 8 19 11 13)
>
> (the actual matrices have hundreds of thousands of entry, that's why I'm
> keen to avoid "for" loops)
>
> And what I need to do is to apply a function which counts the number of
> common elements between ANY row of A and ANY row of B, giving a result
> like this:
>
>
> A1 vs B1:  1  # (8 is a common element)
> A1 vs B2:  1  # (8 is a common element)
> A2 vs B1:  1  # (2 is a common element)
> A2 vs B2:  1  # 11, 13 are common elements
> Etc.
>
> I've built a function that counts the number of common elements between
> two vectors, based on the intersect function in the R manual
>
> common_elements <- function(x,y) length(y[match(x,y,nomatch=0)])
>
> And a double loop who solves my problem would be something like
> (pseudo-code)
>
> For(i in 1:N_A){
>        for(j in 1:N_B){
>                ce(i,j)=common_elements(a(i),b(j))
>                }
>        }
>
> Is there an efficient, clean way to do the same job and give as an
> output a matrix N_A x N_B such as that above?
>
> Thanks a lot for your help
>
> Regards
>
> Pietro
>
> ______________________________________________________________________
>
> For information pertaining to Willis' email confidentiality and monitoring policy, usage restrictions, or for specific company registration and regulatory status information, please visit http://www.willis.com/email_trailer.aspx
>
> We are now able to offer our clients an encrypted email capability for secure communication purposes. If you wish to take advantage of this service or learn more about it, please let me know or contact your Client Advocate for full details. ~W67897
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

```