Vectorization of For loop

1 view (last 30 days)
Pallavi
Pallavi on 9 Aug 2011
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 Comments
Sean de Wolski
Sean de Wolski on 9 Aug 2011
fyi:
c1 = abs(ifft2(fcn));
Sean de Wolski
Sean de Wolski on 9 Aug 2011
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)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 9 Aug 2011
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 Comment
Sean de Wolski
Sean de Wolski on 9 Aug 2011
bs doesn't change, calculate bs*bs once.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!