How do I allocate one point only in matrix A to its closest point in matrix B

3 views (last 30 days)
Hello everyone!
relatively new to MATLAB and would like some help.
I have 2 2D points arrays A, B. I need to connect each point in matrix A to its closest point in matrix B.
The issue is that I can connect just 1 point from A to just 1 point in B. So if a point in A is further away from all the points in B then all other points in A I need to discard it.
This complicates things because it becomes recursive.. and I'm not sure how to implicate this.
If anyone has an idea or advice it will be awesome!
Thanks!

Answers (1)

KSSV
KSSV on 24 Oct 2020
Edited: KSSV on 24 Oct 2020
Read about knnsearch. This function will give you the number points you specified which are close.
idx = knnsearch(B,A) ;
B(idx,:) % these are the points in B close to A
  2 Comments
Guy Inbar
Guy Inbar on 24 Oct 2020
Hey KSSV,
Thanks for the respond!
the problem isnt so much finding the closest point but to devide them all optimliy to couples.
The problem then becomes more complicated because 1 point in A can be closest to 2 points in B.
when i find the second point in B i check which of the, is closest to the point in A and then disconnect the further one. then going back to the point that i disconnected becomes recursive and complicated. ( I have around 300 points in A and B)
KSSV
KSSV on 24 Oct 2020
You need not to bother about that....you specify that you need one point in knnsearch, it will give the closest point.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!