MATLAB: Finding index into an nx2 matrix that is close to a 1×2 vector.


I have an nx2 matrix of latitude and longitude values that represents the route of a car. I also have a latitude/longitude for a point near that route. I want to find the index into the matrix that is closest to the point near the route.
Any suggestions?
NOTE: I used latitude and longitude to better explain the problem. The values aren't really lat/long.

Best Answer

  • search = [0.3, 0.5];
    data = rand(100, 2);
    nData = size(data, 1);
    dist = sum((data - search(ones(nData,1), :)) .^ 2, 2);
    [minDist, minIndex] = min(dist);