Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorizing nested for loops
Date: Mon, 25 May 2009 17:30:05 +0000 (UTC)
Organization: Xoran Technologies
Lines: 29
Message-ID: <gvekit$gm7$1@fred.mathworks.com>
References: <gvei09$3hm$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1243272605 17095 172.30.248.35 (25 May 2009 17:30:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 25 May 2009 17:30:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:542424


"Farhan Rahman" <farban@gmail.com> wrote in message <gvei09$3hm$1@fred.mathworks.com>...
> Hi everyone,
> 
> I'm trying to vectorize a series of nested for loops within a larger script in order to optimize the performance of it. This seems like it should be fairly simple, but after a lot of messing about with it I haven't managed to make much headway. 
> 
> Here's a section of my code that I'm having trouble with:
> 
>     for i=2:nhx-1
>         for j=2:nhy-1
>            Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
>            +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
>            +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
>            -dt*U(i,j)/(hx)*(U(i,j)-U(i-1,j))...
>            -dt*V(i,j)/(2*hy)*(U(i,j+1)-U(i,j-1));
>         end
>     end
> 


help ndgrid


> nhx and nhy are predefined vectors, and U, Unew and P are preallocated matrices of zeros. dt, hx and hy are all static single values.

and V? If V is zero like U  and P, it looks like Unew should evaluate to zero as well.


> 
> Cheers :)