help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Vectorizing for loops .. can't get the hang of it


From: Joza
Subject: Vectorizing for loops .. can't get the hang of it
Date: Tue, 6 Nov 2012 08:31:06 -0800 (PST)

I understand I should REALLY try use vectors instead of for loops ... anyone
care to explain how to do it? I come from a CS programming background so
traditional for loops always seem the easiest to me.

A piece of code I've used recently is below. I couldn't figure out how to
vectorize it. Where do i start?

for i=1:n
                sum = 0.0;
                
                if i==1
                        for j=1:2
                                sum = sum + A(i,j)*x(j);
                        end
                elseif i==n
                        for j=n-1:n
                                sum = sum + A(i,j)*x(j);
                        end
                else
                        for j=i-1:i+1
                                sum = sum + A(i,j)*x(j);
                        end
                end



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Vectorizing-for-loops-can-t-get-the-hang-of-it-tp4646169.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]