user680111
user680111

Reputation: 1011

Comparing rows between two matrices

Is there a fast way of finding which rows in matrix A are present in matrix B? e.g.

m1 = matrix(c(1:6), ncol=2, byrow = T); m2 = matrix(c(1:4), ncol=2, byrow=T);

and the result would be 1, 2.

The matrices do not have the same number of rows (number of columns is the same), and they are somewhat big - from 10^6 - 10^7 number of rows.

The fastest way of doing it, that I know of for now, is:

duplicated(rbind(m1, m2))

Tnx!

Upvotes: 13

Views: 8339

Answers (2)

haoeric
haoeric

Reputation: 1

I created this function which will return the original ID. For example you want to match matrix x to matrix y, it will return the match ID of y.

rowiseMatch2 <- function(x,y){
  require(data.table)
  keycols <- colnames(x)
  x <- cbind(x, id=1:nrow(x))
  y <- cbind(y, id=1:nrow(y))
  m1 = data.table(x)
  setkeyv(m1, keycols)
  m2 = data.table(y)
  setkeyv(m2, keycols)
  m1id <- m1$id
  m2id <- m2$id

  m1$id <- NULL
  m2$id <- NULL

  m <- na.omit(m2[m1,which=TRUE])
  mo <- m2id[m][order(m1id)]

  if(length(mo) == nrow(x)){
    cat("Complete match!\n")
  }else{
    cat("Uncomplete match, match percentage is:", round(length(mo)/nrow(x), 4)*100, "%\n")
  }
  return(as.integer(mo))
}

Upvotes: -1

Matt Dowle
Matt Dowle

Reputation: 59602

A fast way for that size should be :

require(data.table)
M1 = setkey(data.table(m1))
M2 = setkey(data.table(m2))
na.omit(
    M2[M1,which=TRUE]
)
[1] 1 2

Upvotes: 23

Related Questions