-1
可能重复:
Optimization a recurring matlab code需要优化这个matlab代码...向量化会有帮助吗?
是矢量优化这段代码一个很好的选择?无论我们是否将代码矢量化,什么标准决定?还有什么可以做的?
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
矢量化的好坏取决于你想要达到的目标。如果你想要速度,你应该在你的代码上运行profiler,然后开始削减永久占用的线路。 – Jonas