Compare rows of matrices with different dimensions and return a single logical vector

0

I have two coordinate arrays, the A matrix with 83213 rows and two columns (longitude, latitude) and the B matrix with 46886 rows and two columns (longitude, latitude). I would like to compare the coordinates of these two arrays and return a logical vector of size 83213 of type c (FALSE, TRUE, TRUE, ...), being true if the coordinate of matrix A is contained in matrix B.

I know that I can compare vectors of different sizes using the command vector1% in% vector2, but how to compare arrays and the output is just a logical vector? It's possible?

    
asked by anonymous 09.04.2017 / 22:52

1 answer

1

Fixed - In order to run the test simultaneously on an array, you need to check in a second step using the ALL() function:

a <- matrix(1:50, ncol = 2)
b <- a[seq(1,25, 2), ]

a%in%b # o resultado aparenta estar correto

c <- rep(FALSE, 25)

for(i in 1:dim(a)[1]){
  c[i] <- all(a[i,]%in%b)
}

c # resultado correto // TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE ...


## alterando valores de b // todos devem ser falsos
b[,1] <- b[,1] + 1

a%in%b # resultado errado

d <- rep(FALSE, 25)

for(i in 1:dim(a)[1]){
  d[i] <- all(a[i,]%in%b)
}

d # resultado correto // FALSE FALSE  FALSE FALSE  FALSE FALSE ...
    
10.04.2017 / 00:19