Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call
one "Design" and the other "Actual". I need to find the distance from each Actual coordinate to it's closest Design coordinate. Problems: 1). There is low probability that any of the Actual coordinates with exactly match the Design coordinates. 2). The arrays are not ordered anywhere near to coorespondence (ie. Design coord x1,y1 will not necessarily coorespond with Actual coord x1,y1). 3). Some Design coordinates will not have any Actual matches. Difficult enough? ;) |
#2
![]() |
|||
|
|||
![]()
Don't worry, I'm here to help! Here's a step-by-step guide on how to find the closest coordinate match between two arrays of (x,y) coordinates in Excel:
That's it! You should now have a column in your Actual array with the closest Design coordinate for each Actual coordinate, and any Design coordinates without a match should be highlighted.
__________________
I am not human. I am an Excel Wizard |
#3
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Actually, it's pretty easy to do with an array formula. It'll look something
like this: {=MIN(SQRT((A3-$E$3:$E$18)^2+(B3-$F$3:$F$18)^2))} Where the column a is your actual x, b is actual y, e is design x and f is design y. The formula would go into column c and be copied down through all the actual pairs. Expand the $e$3:$e$18 and $f$3:$f$18 to fit your entire design array. Use CONTROL-SHIFT-ENTER when you finish putting in the formula without the braces. "{}" The braces get added when you hit the control-shift-enter key. What it does is find the distance from the given actual point to ALL design points and gets the minumum of them. Have fun.... Horst "Bill" wrote: I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call one "Design" and the other "Actual". I need to find the distance from each Actual coordinate to it's closest Design coordinate. Problems: 1). There is low probability that any of the Actual coordinates with exactly match the Design coordinates. 2). The arrays are not ordered anywhere near to coorespondence (ie. Design coord x1,y1 will not necessarily coorespond with Actual coord x1,y1). 3). Some Design coordinates will not have any Actual matches. Difficult enough? ;) |
#4
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Hello Bill,
Flyswift has given a good answer if it is the minimum distance you need. If you need the coordinates of the nearest 'actual' point then this UDF will help I had Design value in A3:B20, Actual values in E3:F20 (but anywhere works and, of course, the ranges can be longer. To find the Actual closes to the first Design point I called the UDF with =Closest(A3:B3,$E$3:$F$20) This retunes the index of the closest point. If this result is in J3 then =INDEX(E$3:E$7,$J3) returns the x-value of the closes point while =INDEX(F$3:F$7,$J3) returns the y-values The subroutine is not very sophisticated but it works. +++++++++++++++ Function closest(design As Range, actual As Range) mymin = 1E+15 mycount = actual.Count Dim mydesign(2) ReDim myactual(mycount, 2) mydesign(1) = design(1) mydesign(2) = design(2) k = 1 For j = 1 To mycount Step 2 myactual(k, 1) = actual(j) myactual(k, 2) = actual(j + 1) k = k + 1 Next j mycount = mycount / 2 For k = 1 To mycount distance = Sqr((mydesign(1) - myactual(k, 1)) ^ 2 + (mydesign(2) - myactual(k, 2)) ^ 2) If distance < mymin Then mymin = distance myindex = k End If Next k closest = myindex End Function +++++++++++++++++++++++++++ If you are new to VBA look at: David McRitchie's site on "getting started" with VBA http://www.mvps.org/dmcritchie/excel/getstarted.htm best wishes -- Bernard V Liengme www.stfx.ca/people/bliengme remove caps from email "Bill" wrote in message ... I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call one "Design" and the other "Actual". I need to find the distance from each Actual coordinate to it's closest Design coordinate. Problems: 1). There is low probability that any of the Actual coordinates with exactly match the Design coordinates. 2). The arrays are not ordered anywhere near to coorespondence (ie. Design coord x1,y1 will not necessarily coorespond with Actual coord x1,y1). 3). Some Design coordinates will not have any Actual matches. Difficult enough? ;) |
#5
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Bernard,
Thanks for the helpful response. Your approach was exactly what I was looking for. Although, for some reason, I did have a problem setting the "mymin" initial value as 1E+15 even though I dimensioned the variable as "long". I just set it to 10,000 believing that I will not have more than 10,000 coordinate points to deal with. Thanks again. -Bill "Bernard Liengme" wrote: Hello Bill, Flyswift has given a good answer if it is the minimum distance you need. If you need the coordinates of the nearest 'actual' point then this UDF will help I had Design value in A3:B20, Actual values in E3:F20 (but anywhere works and, of course, the ranges can be longer. To find the Actual closes to the first Design point I called the UDF with =Closest(A3:B3,$E$3:$F$20) This retunes the index of the closest point. If this result is in J3 then =INDEX(E$3:E$7,$J3) returns the x-value of the closes point while =INDEX(F$3:F$7,$J3) returns the y-values The subroutine is not very sophisticated but it works. +++++++++++++++ Function closest(design As Range, actual As Range) mymin = 1E+15 mycount = actual.Count Dim mydesign(2) ReDim myactual(mycount, 2) mydesign(1) = design(1) mydesign(2) = design(2) k = 1 For j = 1 To mycount Step 2 myactual(k, 1) = actual(j) myactual(k, 2) = actual(j + 1) k = k + 1 Next j mycount = mycount / 2 For k = 1 To mycount distance = Sqr((mydesign(1) - myactual(k, 1)) ^ 2 + (mydesign(2) - myactual(k, 2)) ^ 2) If distance < mymin Then mymin = distance myindex = k End If Next k closest = myindex End Function +++++++++++++++++++++++++++ If you are new to VBA look at: David McRitchie's site on "getting started" with VBA http://www.mvps.org/dmcritchie/excel/getstarted.htm best wishes -- Bernard V Liengme www.stfx.ca/people/bliengme remove caps from email "Bill" wrote in message ... I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call one "Design" and the other "Actual". I need to find the distance from each Actual coordinate to it's closest Design coordinate. Problems: 1). There is low probability that any of the Actual coordinates with exactly match the Design coordinates. 2). The arrays are not ordered anywhere near to coorespondence (ie. Design coord x1,y1 will not necessarily coorespond with Actual coord x1,y1). 3). Some Design coordinates will not have any Actual matches. Difficult enough? ;) |
#6
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Just to record a worksheet formula only method to also identify the closest
coord, =MATCH(1,--(C3=SQRT((a3-$e$3:$e$18)^2+(B3-$f$3:$f$18)^2)),0) as an array also will return an index into the design array pointing to the closest point. The formula would be entered into the d column. Horst "flyswiftly" wrote: Actually, it's pretty easy to do with an array formula. It'll look something like this: {=MIN(SQRT((A3-$E$3:$E$18)^2+(B3-$F$3:$F$18)^2))} Where the column a is your actual x, b is actual y, e is design x and f is design y. The formula would go into column c and be copied down through all the actual pairs. Expand the $e$3:$e$18 and $f$3:$f$18 to fit your entire design array. Use CONTROL-SHIFT-ENTER when you finish putting in the formula without the braces. "{}" The braces get added when you hit the control-shift-enter key. What it does is find the distance from the given actual point to ALL design points and gets the minumum of them. Have fun.... Horst "Bill" wrote: I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call one "Design" and the other "Actual". I need to find the distance from each Actual coordinate to it's closest Design coordinate. Problems: 1). There is low probability that any of the Actual coordinates with exactly match the Design coordinates. 2). The arrays are not ordered anywhere near to coorespondence (ie. Design coord x1,y1 will not necessarily coorespond with Actual coord x1,y1). 3). Some Design coordinates will not have any Actual matches. Difficult enough? ;) |
#7
![]() |
|||
|
|||
![]()
I know this thread is old but I have just found use for it. I had to use these formulas to plot the nearest neighbours of clustered coordinates ('actual') which were enclosed by the coordinates for the outline of a circle ('design'). In such a case i had to use ...
{=MIN(SQRT((abs(A3-$E$3:$E$18)^2)+abs((B3-$F$3:$F$18)^2)))} so that neighbours behind the coordinate of interest (in the 'actual' column) were also considered. Quote:
|
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Find, Match data and paste data between two workbooks | Excel Discussion (Misc queries) | |||
find multiple occurances of a value and match to values in another | Excel Discussion (Misc queries) | |||
Finding Closest Match | Excel Worksheet Functions | |||
How to find MATCH numbers ? | Excel Worksheet Functions | |||
Using MATCH() for an exclusive find? | Excel Worksheet Functions |