I have a numeric matrix from which I want to retrieve the index given specific values.
I am trying the which()
function to find values in the matrix.
The problem is that some values are found and some are not.
My matrix is as follows:
x_lat <- as.double(seq(48.0 ,60.0, by=0.1))
y_long <- as.double(seq(-10.0 ,2.0, by=0.1))
xv <- as.double(rep(x_lat,each = 121))
yv <- as.double(rep(y_long, 121))
vMatrix <- as.matrix(cbind(xv,yv))
If I want to retrieve the indices where the value -2.3 is TRUE the function returns correctly a vector with the indices where -2.3 appears.
xx<- which(vMatrix==-2.3,arr.ind=TRUE)
> xx
[1] 78 199 320 441 562 683 804 925 1046 1167 1288 1409 1530 1651 1772 1893 2014 2135 2256 2377 2498
[22] 2619 2740 2861 2982 3103 3224 3345 3466 3587 3708 3829 3950 4071 4192 4313 4434 4555 4676 4797 4918 5039
[43] 5160 5281 5402 5523 5644 5765 5886 6007 6128 6249 6370 6491 6612 6733 6854 6975 7096 7217 7338 7459 7580
[64] 7701 7822 7943 8064 8185 8306 8427 8548 8669 8790 8911 9032 9153 9274 9395 9516 9637 9758 9879 10000 10121
[85] 10242 10363 10484 10605 10726 10847 10968 11089 11210 11331 11452 11573 11694 11815 11936 12057 12178 12299 12420 12541 12662
[106] 12783 12904 13025 13146 13267 13388 13509 13630 13751 13872 13993 14114 14235 14356 14477 14598
But for some numbers (that appear in the matrix) the function does not work, e.g.,
xx<- which(vMatrix==-2.2,arr.ind=TRUE)
> xx
integer(0)