Vectorization instead of for loop

1 Ansicht (letzte 30 Tage)
Jason
Jason am 20 Feb. 2017
Kommentiert: Jason am 20 Feb. 2017
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

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 20 Feb. 2017
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);

Weitere Antworten (0)

Kategorien

Mehr zu Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by