Distance from a point to a dataset

Computing this in a memoryless fashion at each position iteration (i.e. treat eas position calculation independetly) may be prohibitively expensive, but a lot of it really depends on the size of your dataset. How many point describe the edge of the road? What's the typical distance between each 'road edge' point compared to your car length / position delta between subsequent car locations? How many car positions are we talking here?

If you can make some assumptions about your road points and car locations, you may be able to speed up computation. For example, if you can assume that all road points monotonically increase in terms of "distance along the road" (i.e., they aren't just randomly-ordered), and if your car location array(s) are sequential, you can probably use information from the previous iteration to constrain or prime your search for the 2-3 nearest 'road points.'

If I were doing this, I might consider fitting an analytical expression to the road curve as a one-time thing. Consider a very simplified example where the edge of the curving road is represented as some distance y given an x-coordinate, y[x]. You could fit a spline (piecewise polynomial) to it using spline: road_y_of_x_pp = spline(road_x_coords, road_y_offsets);

Then, given each car's (x,y)-location, you could evaluate the y-position of the edge of the spline using ppval: road_y_location = ppval(road_y_of_x_ppm, car_x_location);

and directly compare it to the car's y position: car_over_edge = car_y_location < road_y_location;

When you interpolate something, you're essentially solving for some analytical expression's parameters and then evaluate that expression at the desired point; the piecewise polynomical fit is basically doing the analytical fit once so you're not fitting-and-refitting that curve over and over.

There's a million ways to do this, so there's a few ideas. If you've got a road that's winding all over the place this approach may need some work; maybe chunk it into sections.

There's always the MEX approach where you can offload the brute-force calculations to C. I do that anytime it really isn't possible to vectorize things and the MATLAB loops just kill me.

/r/matlab Thread