200 likes | 370 Views
Loops II. For Loop. For <index variable> = <vector of values> execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81. Drawing a Line. for cX = [10 11 12 13 14 15 16 17 18 19 20] putPixel(cX, 10, g) end a better way?.
E N D
For Loop For <index variable> = <vector of values> execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81
Drawing a Line for cX = [10 11 12 13 14 15 16 17 18 19 20] putPixel(cX, 10, g) end a better way? for cX = 10:1:20 putPixel(cX, 10, g) end for cX = 10:20 putPixel(cX, 10, g) end
Adding numbers 1-5 i = 1; sum = 0; while ( i <= 5) sum = sum + i i = i + 1; end sum = 0; for i = 1:5 sum = sum + i end using for loop ?
Adding Elements of a Vector • Given vector v, sum up its contents sum = 0; i = 1; while ( i <= length(v) ) sum = sum + v(i) i = i + 1; end sum = 0; for i = 1:length(v) sum = sum + v(i) end
Minimum Element of a Vector min = inf; index = 0; for i = 1:length(v) if (v(i) < min) min = v(i) index = i end end min = inf; for i = 1:length(v) if (v(i) < min) min = v(i) end end What if we need the location as well?
Drawing a Rectangle putPixel(10, 10, g); putPixel(10, 11, g); putPixel(10, 12, g); putPixel(11, 10, g); putPixel(11, 11, g); putPixel(11, 12, g); putPixel(12, 10, g); putPixel(12, 11, g); putPixel(12, 12, g); putPixel(10, 10, g); putPixel(11, 10, g); putPixel(12, 10, g); putPixel(10, 11, g); putPixel(11, 11, g); putPixel(12, 11, g); putPixel(10, 12, g); putPixel(11, 12, g); putPixel(12, 12, g);
In general, for (x1,y1)-(x2, y2) for cY = y1:y2 for cX = x1:x2 putPixel(cX, cY, g); end end for cX = x1:x2 for cY = y1:y2 putPixel(cX, cY, g); end end
Draw a Right Triangle putPixel(10, 10, g); putPixel(10, 11, g); putPixel(10, 12, g); putPixel(11, 11, g); putPixel(11, 12, g); putPixel(12, 12, g); (x1, y1) (x2, y2)
In General, scanning through x first … (x1, y1) for cX = x1:x2 for cY = ?:y2 putPixel(cX, xY, g); end end (cX, ?) dy (cX, y2) (x2, y2) cX dx
Triangle Solution dx = x2 - x1; dy = y2 - y1; for cX = x1:x2 yStart = y1 + round( cX * dy / dx); for cY = yStart:y2 putPixel(cX, cY, g); end end
All Subset of Size 2 • Given a vector v, list all the possible subsets of elements of v of size 2. One idea: For a given element, list all the other elements together with it, effectively listing all 2-element subsets that contain that element Repeat this for all elements of the vector (set)
First Try For all elements x of v do list all the other elements together with x For all elements x of v do For all elements y of v except x do list (x, y)
First Try … for i=1:length(v) for j=1:length(v) if (i ~= j) disp([num2str(v(i)) ' ' num2str(v(j))]) end end end
First Try? • For v = 1:4, our program produces: 1 2 1 3 1 4 2 1 2 3 2 4 3 1 3 2 3 4 4 1 4 2 4 3
An idea … • For two valid index values k,l, we list two entries : v(k)-v(l) and v(l)-v(k) • One of the listings is extra and not needed • We can put a new rule to apply before making a listing that will eliminate one of the duplicates
Second Try … for i=1:length(v) for j=1:length(v) if (i ~= j && i < j) disp([num2str(v(i)) ' ' num2str(v(j))]) end end end
Eliminate useless iterations • We don’t need to go over all the i,j combinations • Only cases where i < j are used • For a given i value, iterating the j from 1 till i is useless
Solution for i=1:length(v) for j=i+1:length(v) disp([num2str(v(i)) ' ' num2str(v(j))]) end end
Closest to Target • Given a vector v, and a target number n, Find the the separate elements of vector v such that their sum is closest to number n. • Example: v = [10, 5, 20, 7, 13, 3 ] , n=22 The pair whose sum comes closest to 22 is 20 and 3.