How to remove the for loop in this code?

1 回表示 (過去 30 日間)
Shreyas Kamath
Shreyas Kamath 2016 年 5 月 26 日
コメント済み: Shreyas Kamath 2016 年 6 月 15 日
Hi, I am working on a code related to matching score calculation which is provided below:
ref_points=size(tpol,1); %number of rows=number of referenence image points
in_points=size(ipol,1); %number of rows=number of input image points
radsize=7*ones(ref_points,1);
angsize=11* ones(ref_points,1);
radlow=-radsize./2;radhigh=radsize./2;anglow=-angsize./2;anghigh=angsize./2;
epsillon=10;
mscore=0;
for i=1:ref_points
for j=1:in_points
rdiff=tpol(i,1)-ipol(j,1);
ediff=tpol(i,2)-ipol(j,2);
thetadiff=tpol(i,3)-ipol(j,3);
if ((radlow(i) < rdiff) && (rdiff < radhigh(i)) && (anglow(i) < ediff) && ...
(ediff < anghigh(i)) && (abs(thetadiff) < epsillon) && ...
(tpol(i,4)==ipol(j,4)))
mscore=mscore+1;
tpol(i,4)=3; %Change type
end
end
end
end
I am interested to reduce the for loop as it takes high computation time. It would be very helpful if anyone could guide me through this. Thanks in advance.

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 26 日
編集済み: Andrei Bobrov 2016 年 5 月 26 日
r = [-3.5 3.5];
a = [-5.5 5.5];
ep = 10;
ti = bsxfun(@minus,tpol,permute(ipol,[3 2 1]));
p = all([bsxfun(@gt,ti(:,1:2,:),[r(1),a(1)]) & bsxfun(@lt,ti(:,1:2,:),[r(2),a(2)]),...
ti(:,3,:) < ep, ti(:,4,:) == 0],2);
mscore = nnz(p);
tpol(any(p,3),4) = 4;
  4 件のコメント
Shreyas Kamath
Shreyas Kamath 2016 年 5 月 26 日
The first mat file has equal number of ipol and tpol data (same values)as both are same image to be matched. Whereas the second mat file has different data i.e. two different images are being matched.
Shreyas Kamath
Shreyas Kamath 2016 年 6 月 15 日
I was trying to figure out the problem, it seems that
(tpol(i,4)==ipol(j,4)))
provides a different value when compared to
ti(:,4,:) == 0
The logic is correct. But the answer do not match. The rest of the "if conditions" work and provide the same answer. It would be helpful if you could help me solve this.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by