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

Asked

Viewed 384 times

0

I have two coordinate matrices, matrix A with 83213 rows and two columns (longitude, latitude) and matrix B with 46886 rows and two columns (longitude, latitude). I would like to compare the coordinates of these two matrices 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 I can compare vectors of different sizes using the vector 1%in%vector 2 command, but how to compare matrices and the output being just a logical vector? It is possible?

1 answer

1


Corrected - For the test to be done simultaneously in a matrix it is necessary to check in a second step using the function ALL():

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 ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.