Is anyone able to help me increasing the speed of the code below, it is taking too long. Regards.
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;
Looking foward hearing some tips.
This one actually speeds up quite quite nicely using vectorisation (note the use of bsxfun
). I used the fact that exp(A+B)=exp(A)*exp(B)
to compute exp(-(x-xi)^2/(2*BandWidth^2))
separately for x
and y
, and then the summation is handled by normal matrix multiplication, another nice trick. You original code ran in ~5.5 seconds on my computer, this code takes ~0.07 seconds. You do lose a little accuracy for x
and y
near 3.2
and 3.6
, but the difference is still below 1e-14
. My hunch is that it's due to round-off error between exp(A+B)
and 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);