Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X
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
I'm using prime 9
Thank you in advance
Yusra
Solved! Go to Solution.
@Werner_E wrote:
You get different lengths for X.CPT and Y.CPT because you obviously specified wrong ranges!
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.
@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.
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) ...
Stuart
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
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.
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
Exactly I want to do number 2
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)?
#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??
What I expect is the nearest point >>> like take the first point in C and compare it to all points in A and choose the nearest one ... the result will be the same number of points in C ...I need the nearest from all the points not necessarily the first or the last. it will compare each C point to all A points and calculate the distance
BTW, did you really intended to create A and C as those nested matrices?
Could be done much simpler by using>>> All I wanted is showing their values ... they are points (x-axis and y-axis) not matrix ( it seems I did it wrong)
for Xcpt and Ycpt the should be the same length like in excel ..I don't know why they are showed different in mathcad
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)?
It should show them all even if repeated ... C is my reference.. like many different people wear the same clothes ..I need to take the picture of all of them even if they wear the same ( silly example 😉
Thanks in advance , really appreciate it
You get different lengths for X.CPT and Y.CPT because you obviously specified wrong ranges!
@Werner_E wrote:
You get different lengths for X.CPT and Y.CPT because you obviously specified wrong ranges!
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.
@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.
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) ...
Stuart
Oh, sorry, I forgot to note that the version of flatten is tailored more for speed in the task at hand rather than generality.
In most cases we would not be dealing with one row after flattening a nested row; there would usually be multiple rows per nested row. This means the backwards counting time-saving method may not be useable, unless the nesting is regular (ie, same number or flattened rows per nested row. In this latter case, the total number of rows in flattened array can be calculated and a loop (or function) written to iterate over the flattened rows. With an irregular structure, even this isn't feasible without examining all of the rows to determine how many flattened rows there will be. In this case, simplicity is the name of the game, time efficiency can go out of the window, and stack is your friend.
Stuart
I can't thank you enough .. this is exactly what I needed ... and thank you for the thorough explanation. It helps a lot to understand what I'm doing..
Thanks
Yusra
HI stuart @StuartBruff
how did you define A and Ccpt ?... A=? and Ccpt=?
I didn't find them in the screen shot pictures you sent ?
I followed your instructions but I don't know why I didn't get the answers
can you share that part with me please?
Stuart used the data structure for A an d C which you provided in your very first initial posting.
Here A and C were nested matrices, they were n x 1 vectors, consisting of 2x1 matrices with the coordinates.
In your new sheet you flattened the data as I suggested in my answer and now A and C are plain n x 2 matrices.
That's the reason why Stuarts functions don't work anymore as expected and would have to be modified to accommodate the new data structure of the input data.
@Werner_E is right, the change in data format meant the nearest point function no longer worked.
Here's a modified variant of a nearest point function. It should run slightly quicker than the previous version, but I haven't tested that aspect of its performance yet.
2) Choose the nearest point in A to a point in C
I interpret this as, for each point c in C, finding the point a in A with the minimum distance from c.
This is equivalent to iterating through C and for each c search in A for the nearest a to c.
NOTE: The order of A and C matters as the nearest point in A to a point in C isn't necessarily the same as the nearest point in C to a point in A
Also available with real data:
Also matintO takes integer arguments and doesn't know how to cope with array inputs - if you wanted test matrices of the same sizes as A and CCPT, then pass their respective row counts in matintO's first argument position and 2 in the second.
As an aside, the good thing about using matintO is that the values are small and consistent, and you can manually calculate the expected results for small data sets.
However, sometimes, it is better to test with a randomized range of values that is nearer to the target problem.
Initially, I used a=1 and b=10 as arguments for rndints because it was easier to visualise which points were nearer to which. I was too lazy to change rndints to mrunif for the larger values ...
Stuart
Now it works perfectly, Thanks a lot
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 ...
The transpose of the difference in function diff is to convert the difference to a vector thus allowing use the abs operator.
Stuart
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.
Prime 9 file attached
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.
Here is what it might look like if the nearest point is not unique
Prime 9 sheet attached
Thank you terry , I appreciate your help
Hi Werner ..
Thank you so much especially for the drawing ...really appreciate it