MATLAB: Find the closest co-ordinates (between to uneven list of cooardinates)

euclidean distancesfind indexknnsearchpdist2Statistics and Machine Learning Toolbox

I want to select the coordinates in PointinCh1 list which have a coordinates in PointinCh2 list close to them. The code that I am suing at the moment is creating a list called 'closestForPin2toPin1', which is not helpful in finding the indexes in PointinCh1 .
Your help will be appreciated
PointinCh1 =
20 482
19 359
45 438
61 248
90 403
104 95
149 335
148 392
161 73
186 29
188 236
189 319
200 162
208 70
204 198
203 343
214 250
225 307
233 171
238 205
237 245
253 148
264 362
281 34
300 341
306 88
305 203
328 234
326 164
330 20
364 199
424 241
433 314
491 187
PointinCh2 =
99 399
104 95
149 335
148 392
158 82
184 238
190 320
202 343
236 246
263 361
299 342
330 20
493 193
%compute Euclidean distances:
for idis=1: length(PointinCh1)
distances = sqrt(sum(bsxfun(@minus, PointinCh2, PointinCh1(idis,:)).^2,2));
%find the smallest distance and use that as an index into B:
closestForPin2toPin1(idis,:)= PointinCh2(find(distances==min(distances)),:);
end

Best Answer

Read about inbuilt function knnsearch .