Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Vectorizing nested for loops
Date: Mon, 25 May 2009 16:46:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 21
Message-ID: <gvei09$3hm$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1243269961 3638 172.30.248.38 (25 May 2009 16:46:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 25 May 2009 16:46:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1854044
Xref: news.mathworks.com comp.soft-sys.matlab:542420


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

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.

Hopefully someone can help,

Cheers :)