[R] Intersection of 2 matrices

Hans W Borchers hwborchers at googlemail.com
Fri Dec 2 20:46:11 CET 2011


Michael Kao <mkao006rmail <at> gmail.com> writes:

> 
Well, taking a second look, I'd say it depends on the exact formulation.

In the applications I have in mind, I would like to count each occurrence
in B only once. Perhaps the OP never thought about duplicates in B

Hans Werner

> 
> Here is an example based on the duplicated function
> 
> test.mat1 <- matrix(1:20, nc = 5)
> 
> test.mat2 <- rbind(test.mat1[sample(1:5, 2), ], matrix(101:120, nc = 5))
> 
> compMat <- function(mat1, mat2){
>      nr1 <- nrow(mat1)
>      nr2 <- nrow(mat2)
>      mat2[duplicated(rbind(mat1, mat2))[(nr1 + 1):(nr1 + nr2)], ]
> }
> 
> compMat(test.mat1, test.mat2)
>



More information about the R-help mailing list