performance - 优化重复的 matlab 代码

标签 performance matlab optimization if-statement

我正在优化一个模型,该模型获取一些天气数据,然后将云转换为多边形,以便可以进一步利用它们。
该代码可以工作,但速度很慢。通过运行探查器,我发现以下几行被调用 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 MEXFast InPolygon detection MEXHere是原生 matlab 代码,比 matlab inpoly 更快。​​

关于performance - 优化重复的 matlab 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895609/

相关文章:

java - 如何提高这段代码的速度?

matlab - 自动将图像和图表与共享 x 轴对齐

sql - 内联 View 上的谓词推送如何减慢查询速度?

optimization - MySQL 分区性能

ruby-on-rails - Ruby - 如何加速 ".each"数组的循环?

performance - 如何高效地从两个表中选择数据?

performance - 根据欧氏距离对点进行分类 - 优化代码

matlab - 将列添加到元胞数组

matlab - 用以前的非零值替换零值

visual-studio - 命令行错误D8016:'/O2'和'/RTC1'命令行选项不兼容