Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops II. For Loop For = execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81.

Similar presentations


Presentation on theme: "Loops II. For Loop For = execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81."— Presentation transcript:

1 Loops II

2 For Loop For = execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81

3 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

4 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 ?

5 Adding Elements of a Vector Given vector v, sum up its contents sum = 0; for i = 1:length(v) sum = sum + v(i) end sum = 0; i = 1; while ( i <= length(v) ) sum = sum + v(i) i = i + 1; end

6 Minimum Element of a Vector min = inf; for i = 1:length(v) if (v(i) < min) min = v(i) end What if we need the location as well? min = inf; index = 0; for i = 1:length(v) if (v(i) < min) min = v(i) index = i end

7 Drawing a Rectangle 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); 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);

8 In general, for (x1,y1)-(x2, y2) for cX = x1:x2 for cY = y1:y2 putPixel(cX, cY, g); end for cY = y1:y2 for cX = x1:x2 putPixel(cX, cY, g); end

9 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)

10 In General, scanning through x first … (x1, y1) (x2, y2) dx dy cX (cX, y2) (cX, ?) for cX = x1:x2 for cY = ?:y2 putPixel(cX, xY, g); end

11 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

12 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)

13 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)

14 First Try … for i=1:length(v) for j=1:length(v) if (i ~= j) disp([num2str(v(i)) ' ' num2str(v(j))]) end

15 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

16 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

17 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

18 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

19 Solution for i=1:length(v) for j=i+1:length(v) disp([num2str(v(i)) ' ' num2str(v(j))]) end

20 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.


Download ppt "Loops II. For Loop For = execute statements for each value of index end for x = [1 4 7 9] disp(num2str(x^2)); end 1 16 49 81."

Similar presentations


Ads by Google