Vectorization instead of for loop
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!