Vectorization of For loop
显示 更早的评论
Hi every one!
I am trying to vectorize the following for loops as this part of the code is the main bottleneck and taking hours together to get the output
can someone pls help me
Thanks in advance
[rowA colA]=size(blockA);
[rowB colB]=size(blockB);
blockA=double(blockA);
blockB=double(blockB);
m=1;
flag=0;
blockC=zeros([500 4]);
for i=1:rowA
for j=1:rowB
fi = fft2(blockA(i,1:bs*bs));
fr = fft2(blockB(j,1:bs*bs));
% perform phase correlation (amplitude is normalized)
fc = fi .* conj(fr);
fcn = fc ./ abs(fc);
c1 = real(ifft2(fcn));
% find the peak in the correlation plane
[v index] = max(c1(:));
if v>cut_off
blockC(m,1)=blockB(j,bs*bs+1);
blockC(m,2)=blockB(j,bs*bs+2);
blockC(m,3)=v;
blockC(m,4)=2;
flag=1;
m=m+1;
end
end
if flag==1
blockC(m,1)=blockA(i,bs*bs+1);
blockC(m,2)=blockA(i,bs*bs+2);
blockC(m,3)=v;
blockC(m,4)=1;
m=m+1;
flag=0;
end
end
2 个评论
Sean de Wolski
2011-8-9
fyi:
c1 = abs(ifft2(fcn));
Sean de Wolski
2011-8-9
So are you trying to find the displacement field between two images by block processing them and then moving the pieces according to their vector? Please explain the goal.
(A large focus of my MS thesis is in phase correlation displacement fields this piques my interest)
回答(1 个)
Walter Roberson
2011-8-9
Notice that
fi = fft2(blockA(i,1:bs*bs));
does not change as j changes, so there is no need for it to be within the range of the "j" for loop. You can calculate it right after the "i" for loop starts. This will save you quite a number of fft2's.
1 个评论
Sean de Wolski
2011-8-9
bs doesn't change, calculate bs*bs once.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!