フィルターのクリア

improve speed --- image matrix replace

1 回表示 (過去 30 日間)
Bob
Bob 2013 年 5 月 20 日
i have a code which has three "for". it's so slow.
if nx=2000,ny=2000. it takes me two minutes to finish replacement.
xfig(nx,ny,3) is a image matrix.
please help me!!
w0=xfig;
nx=length(xfig(:,1,1));%coordinate x
ny=length(xfig(1,:,1));%coordinate y
w1=w0;
for k=1:4
for x=1:nx
for y=1:ny
w1(x,y,:)=w0(mod1(x+y,nx),mod1(x+2*y,ny),:);
end;
end;
%t=1:1:nx;v=1:1:ny;
%w1(t,v,:)=w0(mod1(t+v,nx),mod1(t+2*v,ny),:);
w0=w1;
end;
% if mod(8,4)=0 then it isn't 0, instead of 4
function T=mod1(x,y)
if mod(x,y)==0
T=y;
else
T=mod(x,y);
end
end
  1 件のコメント
Sean de Wolski
Sean de Wolski 2013 年 5 月 20 日
Have you used the profiler to run your code and identify bottlenecks?

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

回答 (1 件)

David Sanchez
David Sanchez 2013 年 5 月 20 日
1st: initialize w1
w1 = zeros(nx,ny,3);
The two inner loops do not make use of k, the outer loop index. Why do you have them there? Take them out.
for x=1:nx
for y=1:ny
w1(x,y,:)=w0(mod1(x+y,nx),mod1(x+2*y,ny),:);
end;
end;
what's this for?
for k=1:4
%t=1:1:nx;v=1:1:ny;
%w1(t,v,:)=w0(mod1(t+v,nx),mod1(t+2*v,ny),:);
w0=w1;
end;
  1 件のコメント
Bob
Bob 2013 年 5 月 20 日
the outer loops k=1:4 is also important,it means iteration of the w1. I must iterate w1 for more than 50 times. so the speed must improve. now if we run the code
for x=1:nx
for y=1:ny
w1(x,y,:)=w0(mod1(x+y,nx),mod1(x+2*y,ny),:);
end;
end;
20 seconds is necessary. it's so bad. please help me!! i have already had no idea!!

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by