比较两个矩阵之间的行 [英] Comparing rows between two matrices
本文介绍了比较两个矩阵之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在一种快速的方法来查找矩阵B中存在矩阵A的哪些行? 例如
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);
结果将是1、2.
矩阵的行数不相同(列数相同),它们有些大-行数从10 ^ 6-10 ^ 7.
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!
推荐答案
该尺寸的快速方法应该是:
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
这篇关于比较两个矩阵之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文