Performance 提高Matlab代码速度

Performance 提高Matlab代码速度,performance,matlab,for-loop,parfor,Performance,Matlab,For Loop,Parfor,是否有人能帮助我提高下面代码的速度,这太长了。问候 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)

是否有人能帮助我提高下面代码的速度,这太长了。问候

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;

向前看,听一些提示。

减小尺寸。e、 g:

高度=128;宽度=192


精度将相似,但执行时间将更短

使用矢量化(请注意使用
bsxfun
)这一功能实际上可以很好地加快速度。我使用
exp(A+B)=exp(A)*exp(B)
分别为
x
y
计算
exp(-(x-xi)^2/(2*带宽^2))
,然后通过正规矩阵乘法处理求和,这是另一个很好的技巧。您的原始代码在我的计算机上运行约5.5秒,此代码需要约0.07秒。对于
3.2
3.6
附近的
x
y
您确实会损失一点精度,但差异仍然低于
1e-14
。我的直觉是,这是由于
exp(A+B)
exp(A)*exp(B)
之间的舍入误差造成的


仅供参考,这可能更适合我,我可以试试。使用exp()属性的Thx TariqGood point。够了!Thx很多
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);