How to reduce execution time without using FOR loops?
Afficher commentaires plus anciens
while(error>0.01)
iter = iter+1;
% for i = 2:1:xdim-1 % 57 secs
% for j = 2:1:ydim-1
% v_now(i,j) = (v_now(i-1,j)+v_now(i,j-1)+v_now(i+1,j)+v_now(i,j+1))/4;
% end
% end
j = 2:xdim-1;
i = 2:xdim-1;
v_now(i,j) = (v_now(i-1,j)+v_now(i,j-1)+v_now(i+1,j)+v_now(i,j+1))/4;
error = max(max(abs(v_now-v_prev)));
v_prev = v_now;
end;
Réponses (1)
Can replace the computation of v_now with conv2
>> a=rand(5);
>> for i=2:4,for j=2:4,b(i,j)=(a(i-1,j)+a(i,j-1)+a(i+1,j)+a(i,j+1))/4;end,end
>> f=[0 1 0;1 0 1;0 1 0]/4
f =
0 0.2500 0
0.2500 0 0.2500
0 0.2500 0
>> conv2(a,f,'valid')
ans =
0.6302 0.4538 0.5160
0.5800 0.7296 0.7551
0.7276 0.8768 0.8236
>> b
b =
0 0 0 0
0 0.6302 0.4538 0.5160
0 0.5800 0.7296 0.7551
0 0.7276 0.8768 0.8236
Also, when compute error, can avoid the nested max call by using
max(abs(v_now(:)-v_prev(:))
Whether it'll make any appreciable difference, don't know otomh...
Catégories
En savoir plus sur Scope Variables and Generate Names dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!