0

enter image description here

I have a curve cutting through my mesh in 2D. It is a moving to the front with time. I have set of points on this curve (front) and my nodes on the mesh. At each time step I need to find which point on the curve (front) is closest to the nodes on my mesh. In other words for each node in my mesh I would like to know which point on the curve is closest to it. Is there a built in MATLAB function to search for this ? (I am using MATLAB environment)

In the figure the question would be which one is the closest black circle to any of the yellow squares.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Noname
  • 355
  • 2
  • 10
  • Can you give some example code so we know what your variables look like? – Lisa Feb 18 '16 at 11:32
  • @Lisa, I basically have the coordinates of black circles (nX2) matrix and coordinates of yellow squares (mX2) matrix. At the end I expect to get a matrix of size (mX2) that contains for each yellow square (m of them) the coordinates of closest black circle (1X2). – Noname Feb 18 '16 at 12:38

1 Answers1

2

Here is an efficient function to calculate pairwise distances:

function D = sqDistance(X, Y)
    D = bsxfun(@plus,dot(X,X,1)',dot(Y,Y,1))-2*(X'*Y);
end

Assuming circles are the coordinates of the black circles and squares the coordinates of the yellow squares as you described, you can do the following:

% example matrices
circles = rand(5,2);
squares = rand(8,2);

D = sqDistance(squares', circles');    
[~,idx] = sort(D, 2)

closest_points = circles(idx(:,1),:)

closest_points has the same dimension as squares and stores the coordinates of the closest circle for every yellow square.

Lisa
  • 3,365
  • 3
  • 19
  • 30
  • thanks a lot working perfectly fine. i also found this built in function in MATLAB **[k,d] = dsearchn(PInterface,X) ** that gives the distance d and the index of the closest circle k :) – Noname Feb 18 '16 at 13:24