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

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to