[Top][All Lists]

[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);
                elseif i==n
                        for j=n-1:n
                                sum = sum + A(i,j)*x(j);
                        for j=i-1:i+1
                                sum = sum + A(i,j)*x(j);

View this message in context:
Sent from the Octave - General mailing list archive at

reply via email to

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