2

I have

X = (Yt.*sin(W))-(Xt.*cos(W));
Y = (Yt.*cos(W))+(Xt.*sin(W));  % which give the coordinates X and Y. 
X_inv = R.*sin(B_involute); 
Y_inv = R.*cos(B_involute);     % which give the coordinates X_inv and Y_inv.

I need to find the nearest two points between X, Y and X_inv, Y_inv.

lots of thanks from now.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 2
    When coming with question about exercise/homework you should show **your own** attempts before posting it on SO. SO is not a code factory. – Timofey Nov 29 '15 at 15:12
  • I could show my whole program but I just need somebody to suggest me a way/ idea. not a code !! just chill out. by the way, This is not a homework. just trying to learn Matlab well. – Ender A. Rencüzoğulları Nov 29 '15 at 15:15
  • Show what you have already tried, what part of the problem you don't understand. And then we're always glad to help :) – Timofey Nov 29 '15 at 15:17
  • Let me be more clear. I have tried to understand "dsearchn" command. and knnsearch. However, I could not understand. I am stucked with this step. I want to go further but trying to understand in which way. All I am applied was wrong and meaningless. Thus, How could I show this to you? I want to learn how I can find that point? in which way? any recommandation or documents link? I hope I could explain myself. thanks for your trying to help. – Ender A. Rencüzoğulları Nov 29 '15 at 15:23

2 Answers2

1

I'll give you some clues:

  1. Use loops to iterate over each X and Y elements in both matrices.
  2. Use Euclidean Distance to get distance between current points.
  3. Take minimal of all distances that you got.
Community
  • 1
  • 1
Timofey
  • 823
  • 1
  • 8
  • 26
1

You can compute the pair-wise distances efficiently using pdist2

D = pdist2( [X(:) Y(:)], [X_inv(:) Y_inv(:)] );

Once you have the pairwise distances, it is easy to find the minimal distance

[md mi] = min(D(:));

Conver linear index into pair index

[idx, inv_idx] = ind2sub( size(D), mi );

The result

fprintf(1, 'Closest points are [%d]: (%f,%f) -> [%d]: (%f,%f)\n',...
        idx, X(idx), Y(idx), inv_idx, X_inv(inv_idx), Y_inv(inv_idx) );
Shai
  • 111,146
  • 38
  • 238
  • 371