Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing rows between two matrices

Tags:

r

matrix

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!

like image 957
user680111 Avatar asked Feb 16 '12 18:02

user680111


1 Answers

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
like image 173
Matt Dowle Avatar answered Sep 30 '22 09:09

Matt Dowle