Skip to main content
14-Alexandrite
October 25, 2024
Solved

The nearest point

  • October 25, 2024
  • 3 replies
  • 5107 views

Hi everyone, 

 

I'm struggling to write a program in mathcad ... I have A , C  points each one of them has its own x and y coordinate as showing below... what I need is to. 

1) compare the two arrays

2) choose the nearest point in A to C 

3) calculate the distance between the near points

for example if A1 is ( 2, 3) and A2( 10, 30) ...and C1  is ( 6, 4)  and C2 ( 2, 7) .... the program will choose A1 for both C1 and C2 since A1 is nearer to them 

then it will calculate d1 =( A1^2+C1^2)^1/2 ,,, d2= (A1^2+C2^2)^1/2

YA_10963798_0-1729859715217.png

I'm using prime 9 

Thank you in advance 

Yusra

Best answer by StuartBruff

@Werner_E wrote:

You get different lengths for X.CPT and Y.CPT because you obviously specified wrong ranges!

Werner_E_0-1729869998180.png

 


I'm rarely confident enough to opine that something is "obviously" wrong. However, I did notice your comments but left the ranges alone until Yusra came back with her response to your questions. 

 

2024 10 25 B.png

    

@YA_10963798  Please, pretty please, can you change your variable names "rows" and "cols" to something like "nrows" and "ncols"?  I keep forgetting that you've redefined these functions and spend ages hunting down what I think is an error on my part until I finally (re)notice that rows doesn't mean what I think it does.

 

I removed the header information from the "nearest pair" array and passed it instead to one of my Table functions, modified for ORIGIN independence.

 

2024 10 25 C.png

 

2024 10 25 D.png

 

But I did find one variant of my mega unnesting function that we mentioned in the recent Kinematics thread.  The variant was a little too complex for most needs, so I've remodelled it to do basic one-level unnesting.  It's just a glorified version of the one I special-to-type function I posted in the kinematics thread, modified for (what I think is) ORIGIN independence (I hate making things origin independent; it makes my brain hurt, and I'm always hunting for missing ORIGINs). 

 

Anyway.  Rather than rework something that works (not everybody has my vices!) then, it might be a lot easier to do as we discussed on the recent Kinematics thread = hammer any offending nested arrays into shape by flattening them until they see the light and become matrices.  Meet the hammer, suitably modified for ORIGIN independence (I think) ...

 

2024 10 25 E.png

 

Stuart

 

 

3 replies

25-Diamond I
October 25, 2024

Its not clear to me what you actually need.

 

1) is it the two points, one from A and one from C which have the shortest distance from each other. The result would be just ONE pair of points and just ONE minimal distance.

 

2) Or are you trying to loop through the vector C, looking for the one point in A which hast the minimal distance? You may get a different result for each point in C and the overall result would be a matrix with coordinates of A-points and distances with the same number of rows as C.

 

3) Similar to 2) , but the other way round. Are you trying to loop through the vector A, looking for the one point in C which hast the minimal distance? You may get a different result for each point in A and the overall result would be a matrix with coordinates of C-points and distances with the same number of rows as A.

 

From the example you gave its probably #2 ?

 

Another question is, what you expect as the result of THE nearest point if there are more points with the same minimal distance. Should it be just the first point encountered, or should it be the last one encountered or do you need a list of all points with the same distance in this case??

 

BTW, did you really intended to create A and C as those nested matrices?

Could be done much simpler by using

Werner_E_0-1729865763524.png 

This only works because X and Y have the same length.

 

I was a bit confused to see that X.CPT and Y.CPT have a different number of entries.

Werner_E_1-1729865856753.png

For the creation of C.CPT you used the minimial number (in this case the lenght of X.CPT) and so you are ignoring the surplus values in Y.CPT.

Is this really as it was intended and as it should be?

Of course we still can create a coordinate matrix similar to the one you created, but not nested

Werner_E_2-1729866381625.png

 

 

14-Alexandrite
October 25, 2024

Exactly I want to do number 2 

25-Diamond I
October 25, 2024

There still are questions:

 

What about unnesting the A and C matrices. You need them in that nested structure?

 

What about the different sizes of X.CPT and Y.CPT. Is this an error or is it as its supposed to be and only the minimum number of bith should be used?

 

What if there are more than just one point in A with the same minimial distance from a C-point? Which of them should be returned (ar all of them)?

23-Emerald V
October 25, 2024

Here's a brute force and ignorance program in Mathcad Prime 10. You can choose which column(s) you want to actually use or return.  It is blisteringly slow on your data set ...

 

2024 10 25 A.png

 

The transpose of the difference in function diff is to convert the difference to a vector thus allowing use the abs operator.

 

Stuart

25-Diamond I
October 25, 2024

Here is a first attempt to your question.

 

I corrected the wrong rang_5 string so now X.CPT and Y.CPT are equal in length.

I also unnested your A and C matrices as already decribed.

 

But only the first(!) point in A with the minimum distance is returned, not a whole list of all points in A with the same minimal distance.

If you really need a full list of A-points for every point in C, you would have to specify the data format you want the result to be.

 

Werner_E_1-1729870277010.png

 

Prime 9 file attached

25-Diamond I
October 25, 2024

I changed my "getNearest" so it would return ALL points in the second matrix with the same minimum distance.
But in your data there is no point in C.CPT where two or more points in A would have the same minimum distance.
So with this data 'nearest point' is uniquely defined.

Werner_E_0-1729873391899.png

 

Here is what it might look like if the nearest point is not unique

Werner_E_2-1729873878482.png

 

Prime 9 sheet attached

 

 

 

21-Topaz II
October 26, 2024