Lecture 07

Lecture 07 - 14:440:127– Introduction to Computers for...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127– Introduction to Computers for Engineers Notes for Lecture 07 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 Loop Examples 1.1 Example- Sum Primes Let’s 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 we’ll add it to our running total. Note that if a particular number is not prime, we don’t do anything other than advancing to the following number. 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 creating 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) 1.2 Example- Finding The Maximum in a Vector In the previous few examples, we’ve seen cases where we’ve kept a running total or running count as we’ve gone through our loop. However, sometimes, you’ll instead want to keep a ”running maximum,” or something along those lines. For instance, let’s say we wanted to find the largest value in some vector V . Let’s first define our process. At all times, we’ll keep track of the ”maximum so far,” which we’ll save in the variable maxvalue . We’ll loop through each element of the vector, and for each of these elements, compare it to our maxvalue so far . If the number we’re currently looking at is bigger than maxvalue , then that should replace maxvalue with the current element of V since that’s the new largest number we’ve seen so far. If it’s not bigger than maxvalue , then don’t 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 we’ve seen so far, and we’ll have seen every element of the vector. There’s one complication: when we try and compare the first element of the matrix to maxvalue , the variable maxvalue won’t have a value yet and we’ll thus get an error message. To fix this, let’s 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, we’d want to set our initial value to +inf , since every value is smaller than positive infinity....
View Full Document

This note was uploaded on 01/11/2010 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.

Page1 / 14

Lecture 07 - 14:440:127– Introduction to Computers for...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online