Vectorization instead of for loop

1 次查看(过去 30 天)
Hi.
I have an image with about 5000 objects on that I have the centroid (xf,yf) locations for. My aim is to take each one and perform a Gaussian Fit to. I currently use a for - loop and want to see if vectorization speeds it up, but I can't figure out how to.
heres my for - loop code:
l=numel(xf)
delta=5; %half width span of data to perform fit to
for indx=1:l
xrange=xf(indx)-delta:xf(indx)+delta; % create x range
ydata=B(yf(indx),xrange)'; %B=Original Image, so y is the intensity at the xrange positions
xdata=(1:2*delta+1)';
%Now do Gaussian fit
[a(indx),b(indx),c(indx),d(indx),xpeak(indx),ypeak(indx),r2(indx)]=myGaussianFit(double(xdata),double(ydata), b0,c0);
fwhm(indx) = c(indx) * sqrt(log(256));
fwhmSUM=fwhmSUM+fwhm(indx);
data(indx,1)=xf(indx);
data(indx,2)=yf(indx);
data(indx,3)=fwhm(indx);
data(indx,4)=r2(indx);
data(indx,5)=a(indx);
data(indx,6)=b(indx);
data(indx,7)=d(indx);
data(indx,8)=xpeak(indx);
data(indx,9)=ypeak(indx);
end
Thanks Jason

采纳的回答

Sean de Wolski
Sean de Wolski 2017-2-20
Preallocate your data array before the loop.
data = zeros(l,9);
Then populate it directly in the for-loop. The preallocation will be what speeds this up, not vectorization.
[data(indx,1),data(indx,2),data(indx,3),FIXME data(indx,etc)]=myGaussianFit(double(xdata),double(ydata), b0,c0);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by