14:440:127– Introduction to Computers for Engineers
Notes for Lecture 7
Rutgers University, Fall 2010
Instructor Brenda V. Cortez
1
Loop Examples
Lets re
iterate
the loop concept with some examples.
1.1
Example  Sum Primes
Lets say we wanted to
sum all 1, 2, and 3 digit prime numbers
. To accomplish this, we could
loop through all 1, 2, and 3 digit integers, testing if each is a prime number (using the isprime
function). If and only if a particular value is prime, then well add it to our running total. Note
that if a particular number is not prime, we dont do anything other than advancing to the following
number.
Forloop format

for k = VECTOR
% replace capitalized parts
STATEMENTS
% replace capitalized parts
end
total = 0;
for k = 1:999
if(isprime(k))
total = total + k;
end
end
disp(total)
One interesting difference between MatLab and other programming languages is that it uses a
vector to indicate what values a loop variable should take. Thus, if you simply write that x equals
an arbitrary vector rather than a statement like x = 1:100, your program will work fine. Here, we
rewrite the previous example for
summing all 1, 2, and 3 digit prime numbers by first cre
ating a vector of all the prime numbers
from 1 to 999, and simply looping through those values:
total = 0;
for k = primes(999)
total = total + k;
end
disp(total)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
1.2
Example  Finding The Maximum in a Vector
In the previous few examples, weve seen cases where weve kept a running total or running count as
weve gone through our loop. However, sometimes, youll instead want to keep a running maximum,
or something along those lines.
For instance, lets say we wanted to
find the largest value in
some vector V.
Lets first define our process.
At all times, well keep track of the “maximum so far,” which well
save in the variable maxvalue. Well loop through each element of the vector, and for each of these
elements, compare it to our maxvalue so far. If the number were currently looking at is bigger than
maxvalue, then that should replace maxvalue with the current element of V since thats the new
largest number weve seen so far. If its not bigger than maxvalue, then dont do anything. Thus, at
the end of our loop, the variable maxvalue will contain the overall maximum value, since that will
be the largest value weve seen so far, and well have seen every element of the vector.
Theres one complication: when we try and compare the first element of the matrix to maxvalue,
the variable maxvalue wont have a value yet and well thus get an error message. To x this, lets
initially set maxvalue to be inf (negative infinity) since every value is bigger than negative infinity.
Similarly, if we were trying to find the minimum of a vector, wed want to set our initial value to
+inf, since every value is smaller than positive infinity.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Madey
 Physics, loop, builtin function, Vector Operations

Click to edit the document details