I'm optimizing a model which takes some weather data and then converts the clouds into polygons, so that they can be further utilized.
The code is working, but its kinds slow. By running the profiler I was able to found out the following lines are being called 106360430
times and takes about 50 secs to process.
Is there a way I can make these lines more efficient?
function [oddNodes] = pointInPolygon (point,thePolygon)
% determine if a point is in the polygon (faster than matlab "inpolygon"command
polyPoints=size(thePolygon,1); % number of polygon points
oddNodes = false;
j=polyPoints;
x=point(1); y=point(2);
for i=1:polyPoints
if (thePolygon(i,2)<y && thePolygon(j,2)>=y || thePolygon(j,2)<y && thePolygon(i,2)>=y)
if (thePolygon(i,1)+(y-thePolygon(i,2))/(thePolygon(j,2)-thePolygon(i,2))*(thePolygon(j,1)-thePolygon(i,1))<x)
oddNodes=~oddNodes;
end
end
j=i;
end