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
Sean de Wolski 2011 年 8 月 9 日
fyi:
c1 = abs(ifft2(fcn));
Sean de Wolski
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
Walter Roberson 2011 年 8 月 9 日

0 投票

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
Sean de Wolski 2011 年 8 月 9 日
bs doesn't change, calculate bs*bs once.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2011 年 8 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by