[R] Comparing matrices in R - matrixB %in% matrixA

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri Oct 31 15:15:27 CET 2014


Thank you for the reproducible example, but posting in HTML can corrupt your example code so please learn to set your email client mail format appropriately when posting to this list.

I think this [1] post, found with a quick Google search for "R match matrix", fits your situation perfectly.

match(data.frame(t(B)), data.frame(t(A)))

Note that concatenating vectors in loops is bad news... a basic optimization for your code would be to preallocate a logical result vector and fill in each element with a TRUE/FALSE in the outer loop, and use the which() function on that completed vector to identify the index numbers (if you really need that). For example:

lresult <- rep( NA, nrow(A) )
for ( ia in seq.int( nrow( A ) ) ) {
  lres <- FALSE
  ib <- 0
  while ( ib < nrow( B ) & !lres ) {
    ib <- ib + 1
    lres <- all( A[ ia, ] == B[ ib, ] )
  }
  lresult[ ia ] <- lres
}
result <- which( lresult )

[1] http://stackoverflow.com/questions/12697122/in-r-match-function-for-rows-or-columns-of-matrix
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On October 31, 2014 6:20:38 AM PDT, Charles Novaes de Santana <charles.santana at gmail.com> wrote:
>My apologies, because I sent the message before finishing it. i am very
>sorry about this. Please find below my message (I use to write the
>messages
>from the end to the beginning... sorry :)).
>
>Dear all,
>
>I am trying to compare two matrices, in order to find in which rows of
>a
>matrix A I can find the same values as in matrix B. I am trying to do
>it
>for matrices with around 2500 elements, but please find below a toy
>example:
>
>A = matrix(1:10,nrow=5)
>B = A[-c(1,2,3),];
>
>So
>> A
>     [,1] [,2]
>[1,]    1    6
>[2,]    2    7
>[3,]    3    8
>[4,]    4    9
>[5,]    5   10
>
>and
>> B
>     [,1] [,2]
>[1,]    4    9
>[2,]    5   10
>
>I would like to compare A and B in order to find in which rows of A I
>can
>find the  rows of B. Something similar to %in% with one dimensional
>arrays.
>In the example above, the answer should be 4 and 5.
>
>I did a function to do it (see it below), it gives me the correct
>answer
>for this toy example, but the excess of for-loops makes it extremely
>slow
>for larger matrices. I was wondering if there is a better way to do
>this
>kind of comparison. Any idea? Sorry if it is a stupid question.
>
>matbinmata<-function(B,A){
>    res<-c();
>    rowsB = length(B[,1]);
>    rowsA = length(A[,1]);
>    colsB = length(B[1,]);
>    colsA = length(A[1,]);
>    for (i in 1:rowsB){
>        for (j in 1:colsB){
>            for (k in 1:rowsA){
>                for (l in 1:colsA){
>                    if(A[k,l]==B[i,j]){res<-c(res,k);}
>                }
>            }
>        }
>    }
>    return(unique(sort(res)));
>}
>
>
>Best,
>
>Charles
>
>On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
>charles.santana at gmail.com> wrote:
>
>> A = matrix(1:10,nrow=5)
>> B = A[-c(1,2,3),];
>>
>> So
>> > A
>>      [,1] [,2]
>> [1,]    1    6
>> [2,]    2    7
>> [3,]    3    8
>> [4,]    4    9
>> [5,]    5   10
>>
>> and
>> > B
>>      [,1] [,2]
>> [1,]    4    9
>> [2,]    5   10
>>
>> I would like to compare A and B in order to find in which rows of A I
>can
>> find the  rows of B. Something similar to %in% with one dimensional
>arrays.
>> In the example above, the answer should be 4 and 5.
>>
>> I did a function to do it (see it below), it gives me the correct
>answer
>> for this toy example, but the excess of for-loops makes it extremely
>slow
>> for larger matrices. I was wondering if there is a better way to do
>this
>> kind of comparison. Any idea? Sorry if it is a stupid question.
>>
>> matbinmata<-function(B,A){
>>     res<-c();
>>     rowsB = length(B[,1]);
>>     rowsA = length(A[,1]);
>>     colsB = length(B[1,]);
>>     colsA = length(A[1,]);
>>     for (i in 1:rowsB){
>>         for (j in 1:colsB){
>>             for (k in 1:rowsA){
>>                 for (l in 1:colsA){
>>                     if(A[k,l]==B[i,j]){res<-c(res,k);}
>>                 }
>>             }
>>         }
>>     }
>>     return(unique(sort(res)));
>> }
>>
>>
>> Best,
>>
>> Charles
>>
>>
>> --
>> Um axé! :)
>>
>> --
>> Charles Novaes de Santana, PhD
>> http://www.imedea.uib-csic.es/~charles
>>
>
>
>
>-- 
>Um axé! :)
>
>--
>Charles Novaes de Santana, PhD
>http://www.imedea.uib-csic.es/~charles
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list