14-440-127+Lecture+06

14-440-127+Lecture+06 - 14:440:127– Introduction to...

Info iconThis preview shows pages 1–3. 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 Document Right 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 06 Rutgers University, Spring 2010 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- Duplicate each element Now, let’s look at writing a loop to do something we can’t already do in Matlab: duplicating each element of a vector. In other words, given the vector [1 4 3], we want to end up with [1 1 4 4 3 3]. Here are three of many ways we could write this: 1 V = 1:5; % sample vector %%% method 1 for j = 1:length(V) V2((2*j-1):(2*j)) = V(j); end disp(V2) %%% method 2 for j = 1:(2*length(V)) V2(j) = V(ceil(j/2)); end disp(V2) %%% method 3 counter = 0.5; for j = 1:(2*length(V)) V2(j) = V(ceil(counter)); counter = counter + 0.5; end disp(V2) 1.3 Example- Converting A For Loop to a While Essentially every for loop can be written as a while loop. This is a three step process: • Notice that we need to initialize a loop variable (a while loop does not do this automatically). If our for loop began for x = 1:2:15 , we must state that x = 1 initially, before our while loop begins. • You must create a condition that is true while you want the loop to keep looping, and that becomes false when you want the loop to stop. Usually, this is the upper (or lower) bound on your loop variable. In our example, we’d want to keep looping while x is less than or equal to 15: x < = 15 . • Finally, before each iteration of our while loop ends, we must increment our loop variable. Notice that a for loop did that for us. Right before your end statement, you’ll likely place something like x = x+2 if you were converting the above example. Here’s this simple example, written as both a for loop and an equivalent while loop ....
View Full Document

{[ snackBarMessage ]}

Page1 / 10

14-440-127+Lecture+06 - 14:440:127– Introduction to...

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

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