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.

카테고리

도움말 센터File Exchange에서 Loops 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