How to reduce the time complexity?

1 回表示 (過去 30 日間)
.. Vamshi
.. Vamshi 2013 年 2 月 5 日
The following code is taking much time to execute. What I wanted to do is.. Dividing the matrix of size 90x90 into 9x9 grids and calculating the angle counts in each range. Its taking a lot of time for computation. But I need to reduce this at any cost. Could not find the solution.
fsrc = sprintf('FeatureVector%d',ii);
%FeatureVector=zeros([100,8]);
cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;
p = 1;
row = 1;
rlength = size(Gdir,1);
clength = size(Gdir,2);
while p<(rlength)
q = 1;
while q<(clength)
for i=p:p+8
for j=q:q+8
if Gdir(i,j)>=-180 && Gdir(i,j)<=-134
cnt1 = cnt1+1;
elseif Gdir(i,j)>=-135 && Gdir(i,j)<=-89
cnt2 = cnt2 + 1;
elseif Gdir(i,j)>=-90 && Gdir(i,j)<=-44
cnt3 = cnt3 + 1;
elseif Gdir(i,j)>=-45 && Gdir(i,j)<=-1
cnt4 = cnt4 + 1;
elseif Gdir(i,j)>=0 && Gdir(i,j)<=45
cnt5 = cnt5 + 1;
elseif Gdir(i,j)>=46 && Gdir(i,j)<=90
cnt6 = cnt6 + 1;
elseif Gdir(i,j)>=91 && Gdir(i,j)<=135
cnt7 = cnt7 + 1;
else
cnt8 = cnt8 + 1;
end
eval([fsrc '(row,1) = cnt1;']);
eval([fsrc '(row,2) = cnt2;']);
eval([fsrc '(row,3) = cnt3;']);
eval([fsrc '(row,4) = cnt4;']);
eval([fsrc '(row,5) = cnt5;']);
eval([fsrc '(row,6) = cnt6;']);
eval([fsrc '(row,7) = cnt7;']);
eval([fsrc '(row,8) = cnt8;']);
end
end
q = q + 9; row = row+1;
cnt1=0; cnt2=0; cnt3=0; cnt4=0; cnt5=0; cnt6=0; cnt7=0; cnt8=0;
end
p = p + 9;
end
dst = sprintf('FV%d.mat',ii);
s = ['save ./FeatureVectors/' dst ' ' fsrc ';'];
eval(s);

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 2 月 5 日
Well first off, don't use eval(); it's evil and slow.
Second, considering that row never changes, you don't need to icncrement row each time. But then again this is what I would do instead of using eval and cnt1:cnt5
if Gdir(i,j)>=-180 && Gdir(i,j)<=-134
fsrc(row,1) = fsrc(row,1)+1;
elseif Gdir(i,j)>=-135 && Gdir(i,j)<=-89
etc.
Also, it looks like you're just doing a histogram:
fsrc = histc(Gdir(:),[-180 -134 -89 etc.])
And, most importantly, use the profiler to profile your code and identify bottlenecks.
  1 件のコメント
.. Vamshi
.. Vamshi 2013 年 2 月 6 日
Oh..Ya Thanks alot. I'll try the suggested changes.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by