Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorizing nested for loops
Date: Tue, 26 May 2009 00:56:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 29
Message-ID: <gvfen2$239$1@fred.mathworks.com>
References: <gvei09$3hm$1@fred.mathworks.com> <gveme8$cqh$1@fred.mathworks.com> <gvemma$s97$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 1243299362 2153 172.30.248.38 (26 May 2009 00:56:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 26 May 2009 00:56:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1854044
Xref: news.mathworks.com comp.soft-sys.matlab:542465


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gvemma$s97$1@fred.mathworks.com>...
> Sorry, actually you do not need bsxops, just do this:
> 
> > i = 2:nhx-1;
> > 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));
> > 

Thank you so much, that was much simpler than I was trying to make it and it worked like a charm :)

Along similar lines then, would it be possible at all to do something similar with a loop in which there were two matrices reliant on the results of each other for each step of the loop? Specifically, this code:

        for i=2:nhx-1
            for j=2:nhy-1
                resid(i,j)= -1/(hx*hx)*(Pc(i+1,j)-2.*Pc(i,j)+Pc(i-1,j))...
                -1/(hy*hy)*(Pc(i,j+1)-2.*Pc(i,j)+Pc(i,j-1))...
                +1/dt*div(i,j);
                Pc(i,j)= (1/(-2/(hx*hx)-2/(hy*hy))*resid(i,j))*relax_pc+Pc(i,j);
            end
        end

I can't seem to separate the loops without getting incorrect answers, which leaves me in a bit of a pickle with regards to vectorization. I think I can eliminate one of these loops but I'm not sure that the performance gain from that alone is worth the change.

Thanks again :)