Carl Witthoft
2008-Dec-09 23:49 UTC
[R] Better way to find distances between points in a set?
I was playing around a bit to see how I could find the two points in a set of points (or ordered pairs) furthest from each other. Here's what I did: 1) created a Nrow by 2col matrix, so each row contains an x,y coordinate pair. 2) fed the matrix to a nested mapply (cv is my matrix): mapply(function(k,l) mapply(function(x,y,a,b) + sqrt((x-a)^2+(y-b)^2),cv[,1],cv[,2],k,l),cv[,1],cv[,2])->alldist Then I just did which.max(alldist) and found the original two points by figuring out what row, col the results of which.max referred to. So, what's a better, or cleaner way to do all this? That is, is there a function in some package that will do anything like my nested mapply thing, and is there a better tool than which.max for locating a position in a matrix? thanks Carl
Charles C. Berry
2008-Dec-10 04:52 UTC
[R] Better way to find distances between points in a set?
The 'better way' to do almost anything starts with a reading of the _posting guide_, which reminds you to Do your homework before posting [Reasons whyfor deleted]] * Do help.search("keyword") and apropos("keyword") with different keywords (type this at the R prompt). [other homework items deleted] So, to start with you would have tried: help.search('distance') HA! This leads to dist {stats} Distance Matrix Computation Well, doesn't that sound promising??> alldist3 <- as.matrix( dist( cv ) ) > which( alldist3 == max( alldist3 ), arr.ind=TRUE )Oh yes, if you are too lazy to look up the posting guide URL, the function help.request() will open it for you when you admit that you haven't yet read it (or lead you thru the further steps to prepare a question to this list if you say that you have read it). HTH, Chuck On Tue, 9 Dec 2008, Carl Witthoft wrote:> I was playing around a bit to see how I could find the two points in a set of > points (or ordered pairs) furthest from each other. > > Here's what I did: > 1) created a Nrow by 2col matrix, so each row contains an x,y coordinate > pair. > > 2) fed the matrix to a nested mapply (cv is my matrix): > > mapply(function(k,l) mapply(function(x,y,a,b) > + sqrt((x-a)^2+(y-b)^2),cv[,1],cv[,2],k,l),cv[,1],cv[,2])->alldist > > Then I just did which.max(alldist) and found the original two points by > figuring out what row, col the results of which.max referred to. > > So, what's a better, or cleaner way to do all this? That is, is there a > function in some package that will do anything like my nested mapply thing, > and is there a better tool than which.max for locating a position in a > matrix? > > thanks > Carl > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego famprevmed.ucsd.edu/faculty/cberry La Jolla, San Diego 92093-0901
cgw at witthoft.com
2008-Dec-10 16:58 UTC
[R] Better way to find distances between points in a set?
Hah! I DID do my homework first. From now on I guess I'll have to post the complete list of trails I followed before deciding I was lost. That appears to be the only way to stave off the Harpies. So, before I posted, and before I even wrote my toy script, I found and read thru the help file for dist(). If in fact dist() does what I want, I couldn't see that from either the description or the example. Or from an example I tried myself. Remember: my Euclidean distance requires first finding the difference between the orthogonal components of the two points, i.e. (x1-x2) and (y1-y2) and then calculating the Pythagorean of those values. Just getting distances between the actual elements of my matrix (x and y components) isn't the same. And, yes, I tried 'euclidean' . So I would be greatful indeed if someone could confirm that, for a matrix as I described, where each row contains the coordinates of a single point, that dist() can be convinced to return the geometric euclidean distances. Or that it can't. Carl PS I also found which and which.max all by myself! ^_^ Dec 9, 2008 11:52:46 PM, cberry at tajo.ucsd.edu wrote: ========================================== The 'better way' to do almost anything starts with a reading of the _posting guide_, which reminds you to Do your homework before posting [Reasons whyfor deleted]] ? ? * Do help.search("keyword") and apropos("keyword") with different keywords (type this at the R prompt). [other homework items deleted] So, to start with you would have tried: help.search('distance') HA! This leads to dist {stats} Distance Matrix Computation Well, doesn't that sound promising?? > alldist3 <- as.matrix( dist( cv ) ) > which( alldist3 == max( alldist3 ), arr.ind=TRUE ) Oh yes, if you are too lazy to look up the posting guide URL, the function help.request() will open it for you when you admit that you haven't yet read it (or lead you thru the further steps to prepare a question to this list if you say that you have read it). HTH, Chuck On Tue, 9 Dec 2008, Carl Witthoft wrote: > I was playing around a bit to see how I could find the two points in a set of > points (or ordered pairs) ? furthest from each other. > > Here's what I did: > 1) created a Nrow by 2col matrix, so each row contains an x,y coordinate > pair. > > 2) fed the matrix to a nested mapply (cv is my matrix): > > mapply(function(k,l) mapply(function(x,y,a,b) > + sqrt((x-a)^2+(y-b)^2),cv[,1],cv[,2],k,l),cv[,1],cv[,2])->alldist > > Then I just did which.max(alldist) and found the original two points by > figuring out what row, col the results of which.max referred to. > > So, what's a better, or cleaner way to do all this? ? That is, is there a > function in some package that will do anything like my nested mapply thing, > and is there a better tool than which.max for locating a position in a > matrix? > > thanks > Carl > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > Charles C. Berry ? ? ? ? ? ? ? ? ? ? ? ? ? ? (858) 534-2098 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu ? ? ? ? ? ? UC San Diego famprevmed.ucsd.edu/faculty/cberry ? La Jolla, San Diego 92093-0901