我正在优化一个模型,该模型获取一些天气数据,然后将云转换为多边形,以便可以进一步利用它们。
该代码可以工作,但速度很慢。通过运行探查器,我发现以下几行被调用 106360430
次,处理时间大约为 50 秒。
有没有办法让这些生产线更有效率?
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
最佳答案
inPolygon
测试是一个繁重的函数,可能最好在 mex 文件中完成。您可以查看以下一些 FEX 贡献:inpoly-mex-file , Fast Inpolygon in MEX和 Fast InPolygon detection MEX 。 Here是原生 matlab 代码,比 matlab inpoly
更快。
关于performance - 优化重复的 matlab 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895609/