Vectorization instead of for loop

2 ビュー (過去 30 日間)
Jason
Jason 2017 年 2 月 20 日
コメント済み: Jason 2017 年 2 月 20 日
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);
  1 件のコメント
Jason
Jason 2017 年 2 月 20 日
Thankyou

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCurve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by