繁体   English   中英

提高 Matlab 代码速度

[英]Increase Matlab code speed

有没有人能帮我提高下面代码的速度,它花费的时间太长了。 问候。

limits(1) = 3.2;    
limits(3) = 3.6;
x = ones(426,1);   
y = ones(426,1);
BandWidth = 20;
height = 586;
width = 896;

dmap = zeros(height, width);
parfor  jj = 0 : width - 1
    myTemp = zeros(1, height);
    xi = limits(1) + jj;
    for ii = 0: height - 1
        yi = limits(3) + ii;
        myTemp(ii+1) = sum( exp(-((x - xi).^2 + (y - yi).^2)/(2*BandWidth^2)) );
    end
    dmap(:,jj+1) = myTemp';
end
dmap = (1/(sqrt(2*pi)*BandWidth))*dmap;

期待听到一些提示。

减少尺寸。 例如:

高度 = 128; 宽度 = 192

准确性将相似,但性能时间会更少。

这个实际上使用矢量化可以很好地加速(注意使用bsxfun )。 我使用exp(A+B)=exp(A)*exp(B)xy分别计算exp(-(x-xi)^2/(2*BandWidth^2))的事实,然后求和由普通矩阵乘法处理,这是另一个不错的技巧。 您的原始代码在我的计算机上运行约 5.5 秒,此代码需要约 0.07 秒。 xy3.23.6附近确实失去了一点精度,但差异仍然低于1e-14 我的直觉是,这是由于exp(A+B)exp(A)*exp(B)之间的舍入误差造成的。

limits(1) = 3.2;    
limits(3) = 3.6;
x = ones(426,1);   
y = ones(426,1);
BandWidth = 20;
height = 586;
width = 896;
xi=limits(1)+(0:width-1);
yi=limits(3)+(0:height-1);
X=exp(-bsxfun(@minus,x,xi).^2/(2*BandWidth^2));
Y=exp(-bsxfun(@minus,y,yi).^2/(2*BandWidth^2));
dmap=Y.'*X/(sqrt(2*pi)*BandWidth);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM