杀杀


得到结果是4和5都在两个矩阵中出现。
但是有时候我们想取多列的交集,比如如下的矩阵:
此时intersect是不能适用的。
在matlab中可以直接使用函数取两列的交集:

duplicated R语言取交集
在R中我们可以使用duplicated函数去取这种两列-两列的交集,甚至可以取两列以上的交集,我们需要先把我们需要取交集的两个矩阵通过rbind合并在一起(注意是rbind)
接着使用函数
test <- rbind(mat1,mat2)
intersect_result <- test[duplicated(test), ]
intersect_result

就能得到交集啦~
来看看多列的结果

祝大家521快乐哟~
网友评论