Skip to content
Prev 276376 / 398506 Next

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

Jim

I tried that and it works. Thank you very much for your help!

Regards

Pietro


-----Original Message-----
From: jim holtman [mailto:jholtman at gmail.com] 
Sent: 04 November 2011 13:38
To: Parodi, Pietro
Cc: r-help at r-project.org
Subject: Re: [R] 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:

  
    
Message-ID: <0A760D5925AC5E4893BDDDAABC44E29801EF6F65@USNSH-I-EC80.int.dir.willis.com>
In-Reply-To: <CAAxdm-7d9_jOmZ_C96qzT+85EmGhGusyB+Ef1TGq49S8D0xPwg@mail.gmail.com>