On Apr 29, 2010, at 1:58 PM, YasirKaheil wrote:> Hi,
> I have a matrix of X and Y coordinates, and one pair of coordinates
> that I
> need to know it corresponds (or is closest) to which row in the
> matrix. For
> example:
> MC<- cbind(rep(1:1000,each=1000),rep(1:1000,1000));
> p<-c(543.1,440.05);
>
> I know that if you do which.min you'll get the answer, which will be
> row #
> 542440, but which.min is slow and this process needs to be repeated
> a lot.
It didn't seem to be particularly slow when I tried it:
> which.min( abs(MC[,1]-p[1])+ abs(MC[,2]-p[2]) ) # L1 metric
[1] 542440
system.time( which.min(abs(MC[,1]-p[1])+ abs(MC[,2]-p[2])) )
user system elapsed
0.068 0.034 0.103
What was your code and experience with it?
> I tried using match and findInterval and %in% but I don't know how I
> can use
> them to find in 2 columns simultanuously.
--
David Winsemius, MD
West Hartford, CT