Download Matlab Chapter 3, with some basic skill. and more Slides Computer Science in PDF only on Docsity!
Chapter 4 Loops
• The while Loop
• The for Loop
• Logical Arrays and Vectorization
• Additional Examples
4.1 The while Loop
• A while loop is a block of statement
– Repeating indefinitely as long as some
condition is satisfied.
while expression
end
Code block
while expr
end
pseudcode
A fatal (致命的) flaw (缺點)
• We did not completely test the program for
all possible types of inputs
– Division-by-zero error
4.2 The for Loop
• The for loop is a loop
– Executing a block of statements a specified number of
times.
for index = expr
Statement 1
Statement n
end
Body
index is the loop variable and expr is the loop control expression
expr = first:incr:last
for i = 1:10 Statement
… Statement n
end
for i = 1:2:10 Statement 1
… Statement n
end
i=10 i=
index is the loop variable and expr is the loop control expression expr = first:incr:last for i = 1: Stat eme nt 1 … Stat eme end^ nt n
for i = 1:2: Stat eme nt 1 … Stat eme end^ nt n
i=10 i=
for i = [5 9 7] Stat eme nt 1 … Stat eme end^ nt n
i=
for i = [1 2 3; 4 5 6] Statement 1 … Statement n end = 6 i^3 Loop index can be a vector
Example 4.3—Calculating the Day of Year (Page 158)
- • ordinary years: 1~365leap years: 1~
- – YearsYears evenly divisible by evenly divisible by 400100 are leap yearsbut not by 400 are not leap years
- – All years divisible byAll other years are not leap years 4 but not by 100. are leap years
- Input – year, month, day
- Is leap year? – mod, if, elseif, else, end
- Compute day of year – for ii = 1:month- 1
- switch (ii), case(1, 3, 5, 7, 8, 10, 12)
Detail of for operation
- • Indent the bodies of loopsDon’t modify the loop index within the body of a loop
- Preallocating arrays – arr = 1:4; %=[1 2 3 4]
- – arr(8) = 6;When an array is extended %=[1 2 3 4 0 0 0 6]
- • Create a new arrayCopy the contents of the old array to the new longer array
- • Add the new value to the arrayDelete the old array
- – Vary timeSolution -consuming for long arrays
- Vectorizing Arrays^ •^ Array is preallocated to its maximum size before the loop
- Vectorization^ –^ Vectorized more than^15 times faster than the^ for^ loop
- Replacing loops by vectorized satements
for ii = 1:100 square(ii) = ii^2; square_root = ii^(1/2); cube_root(ii) = ii^(1/3); end ii = 1:100 square(ii) = ii.^2; square_root = ii.^(1/2); cube_root(ii) = ii.^(1/3);
300 separate lines of code
4 lines of code
The break and continue statements
- break
- Terminates the execution of a loop
- Passes control to the next statement after the end of the loop
- continue
- Terminates the current pass through the loop
- Returns control to the top of the loop
for ii = 1:5 if ii == 3; end^ break; end^ fprintf(‘ii = %d\n’, ii); disp([‘End of loop!’])
test_break ii = 1 ii = 2 End of loop!
for ii = 1:5 if ii == 3; end^ continue; end^ fprintf(‘ii = %d\n’, ii); disp([‘End of loop!’])
test_continue ii = 1 ii = 2 ii = 4 ii = 5 End of loop!
Nesting Loops
• If one loop is completely inside another one,
the two loops are called nested loops
for ii = 1:3 for jj = 1: product = ii * jj; fprintf(‘%d * %d = %d\n’, ii, jj, product); end^ end
test_nested_loops 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4.3 Logical Arrays and Vectorization
- Logical arrays have a very important special property - They can serve as a mask for arithmetic operations - A mask is an array that selects the elements of another array for use in an operation. - The specified operation will be applied to the selected elements, and not to the remaining elements.
This is a very fast and very clever way
of performing an operation on a subset of an array
without needing loops and branches
for ii = 1:size(a,1) for jj = 1:size(a,2) if a(ii,jj) > 5; a(ii,jj) = sqrt(a(ii,jj)); end ;product = ii * jj; end^ ;fprintf(‘%d * %d = %d\n’, ii, jj, product); end
Vectorized approach is faster b = a > 5; a(b) = sqrt(a(b));