From: Gabriel on 24 Jul 2010 15:39 Hi, I am interested in learning how to reduce the simulation time for a nested for loop so that I can reduce some computational time. The purpose of my code is to create a 2D staggered grid (j,k) where U is defined at (j+1/2,k), V is defined at (j,k+1/2), and h is defined at (j,k). Most of the computational time of the code is taken doing nested for loops like this one: U=zeros(n,n,3);V=zeros(n,n,3);Z=zeros(n,n,3);PHI=zeros(n,n,3); for L=1:3 for j=1:n-1 for k=1:n-1 U(j+1,k,L)=0.5*(h(j+1,k,L)+h(j,k,L))*u(j+1,k,L); V(j,k+1,L)=0.5*(h(j,k+1,L)+h(j,k,L))*v(j,k+1,L); end end end I wanted to know if there is a way to vectorize this loop. Thanks.
From: Matt J on 24 Jul 2010 16:02 U=convn(h,[0.5;.5],'valid').*u(2:end,:,:); V=convn(h,[0.5,.5],'valid').*v(:,2:end,:);
|
Pages: 1 Prev: Counting the frequency of data in an array Next: fminunc, fminsearch |