1. Iterative statements : a for loop ************************* A for loop is used when you know in advance that a sequence of statements MUST be repeated a GIVEN number of times (eg 10) ************************* - ex0.m Print the numbers from 1 to 3 ... or 1 to 100 - for1.m A simple for loop for ii=1:10 disp(ii); end for ii= 1:10 means % Generate a row vector 1:10 % variable ii takes the values of the row vector one by one from the first to the last % each time the sequence of statements betweent he for and matching end is executed % until control reaches end and then the next value of ii is taken from the rowvector for % the loop to continue % As soon as the last value of the row vector is assigned to ii (say 10 in our example above) % the disp(ii) statement is executed for the last (10th time) and the for loop stops at end % and exits the for loop by executing the statement following the end (in our case nothing). - Syntax of a for loop in for2syn.m or sub7p7for.m - for3.m an if statement in the for loop - for4.m you and me three times (thrice) - Nested for loops nested1.m and nested2.m A break STOPs the execution of the for loop completely and control goes to the statement immediately following the matching end disp() before and after the break! - Continue ccontinue.m and ccontinue1.m A continues STOP the execution of the CURRENT loop only and control goes to the end statement to return to the top so that ii gets the next value from the row vector. Code continues with the execution of the next iteration of the loop (if any left). - danger.m always preallocate arrays never allow them to grow dynamically. It's inefficient and slow. - a= ones(1,10); for ii=a disp(ii) end This forloop is still executed 10 times. Variable ii however takes the same value of 1 in each one of the 10 times disp(ii) is executed! Just because you have a for loop it does not mean that ii must take (in this example,) 10 different values. The values can be the same i.e. arbitrarily chosen in general. - Fibonacci numbers fib.m and sub7p8fibfor.m - nested for loops nested1.m nested2.m nested3.m sub7p9break.m sub7p9continue.m 3. Iterative statements : a while loop ************************* A while loop is used when you know in advance that a sequence of statements MUST be repeated some number of times until a condition becomes true or false but you don't know the exact number of times ************************* - Syntax of a while sub7p2awhile.m - Count to 10 : sub7p2bwhileex.m - Count to 3 : ex1.m ex2.m. ex3.m : 3 while loops - ex1.m Details 1. limit=4; 2. i=1; 3. while (i < limit) 4. fprintf(' %d\n',i); 5. i=i+1; 6 end 7 Line i i 3 2 2 < 4 : t 4 2 2 5 3 6-> 3 3 3 < 4 : t 4 3 3 5 4 6-> 3 4 4 < 4 : f 7 How many times was line 3 executed ? 4 How many times was line 4/5 executed ? 3 - Print only odd numbers ex4.m - Fibonacci sub7p4fib.m - sub7ex1.m : find max and index with max value for vector A - sub7ex2.m : for loop - sub7ex3.m : for loop - sub7ex4.m : while loop - sub7extra5.m sub7extra5a.m : Past Quiz - sub7you.m : Past Quiz 4. More on for and while loops How much is the sum S= 1 + 2 + ... + 100 ? S=100+ 99+ ... + 1 ? 2S=(1+100)+(2+99)+ ... + (100+1) = =101 + 101 + ... + 101 = 100 x 101 = i.e S= 100 x 101 /2 In general S= 1+ 2 + ... + n = n(n+1)/2 = last term * next-to-last /2 Write a program that computes S from 1 to 100 x=1:100; sum(x) disp(ans) mysum=0; for ii=1:100 mysum= mysum+ii; end disp(mysum); mysum=0; ii=1; while (ii ? 100) mysum = mysum +ii; end disp(mysum); Replace ? with the correct relational operator! 5. b=a>5 ! Selected array operations a= [ 10 20 3; 4 50 6; 7 80 9] - What is the maximum of a? Ans: max(max(a)) - How many elements greater than 5 b= a>5 sum(sum(b)) or the cuter length(a(b)) a(b) vectorizes (i.e. produces a vector) of the elements of a in column-major order that satisfy condition b , i.e. they are greater than 5! - For loop :matrix form total=0 for ii=1: size(a,1) for jj=1: size(a,2) if a(ii,jj) > 5 total=total+1; end end end disp (total) - For loop :array form total=0 for ii=1: size(a,1) * size(a,2) if a(ii) > 5 total=total+1; end end disp (total) - Change all >5 entries to half of original value b= a>5; a(b) = a(b) ./ 2 Well, i changed my mind. restore original values a(b) = a(b) .* 2 (This is possible because b keeps track of the locations of the original values that were greater than 5!) b 1 1 0 0 1 1 1 1 1 FILES ****** FOR LOOPS ********** ex0.m : print 1 to 3; for1.m: print 1 to 3 for style for2syn.m: SYNTAX FOR sub7p7for.m for3.m : more for loop for4.m : more for loop danger.m: Resizing arrays !!! nested0.m : nested for loop nested1.m : nested2.m : nested CONTINUE ccontinue.m ccontinue1.m sub7you.m : CONTINUE bbreak.m: brreak1.m: : BREAK nested3.m : ... CONTINUE ... BREAK ? WHILE LOOPS *********** sub7p3awhhile.m : SYNTAX WHILE ex1.m : 1 2 3. ex2.m : smae ex3.m : same ex4.m : odd numbers fib.m : fibonacci fib2.m: int32 fibonacci sub7ex1.m : Find MAX sub7ex2.m : for loop find max sub7ex3.m : fancy for loop find max sub7ex4.m : A(10) =27 ? EXAMPLES (MATLAB DIARY) *********************** 1. Print 1 to 10 (one per row) a= 1:10 a = Columns 1 through 9 1 2 3 4 5 6 7 8 9 Column 10 10 a' ans = 1 2 3 4 5 6 7 8 9 10 2. Print hello 4 times a='hello' a = hello b= [ a ; a ; a ; a ] b = hello hello hello hello for ii = 1:4 disp('hello'); end hello hello hello hello for ii=1:4 ; disp('hello'); end hello hello hello hello for ii=1:4 disp('hello'); end hello hello hello hello ii ii = 4 for i= 1:2:0 ; disp('hello'); end for ii= [ 1 1 1 1 ] disp('hello'); end hello hello hello hello ii ii = 1 for ii= zeros(1,4) ; disp('hello'); end hello hello hello hello for ii= zeros(4,1); disp('hello'); end hello for ii=1:4 disp('hello'); ii = 10; end hello hello hello hello ii ii % the vector generated on the right of = in the for loop does not need to % generate different values... for ii= [ 1 1 1 1 ]; disp(10); end 10 10 10 10 % or non zero values for ii= zeros(1,5) ; disp(10); end 10 10 10 10 10 % if the vector is empty the lines of the body (between for and end) are % not executed. for ii= 1:2:0 disp(10); end 1:2:0 ans = Empty matrix: 1-by-0 4.% A more complex for loop. the body has an if else statement type for3.m for ii=1:10 if (mod(ii,2) == 0) fprintf('ii=%d\n',ii); else fprintf('ii=%d is odd\n',ii); end end mod(3,2) ans = 1 mod(2,2) ans = 0 type for3.m for ii=1:10 if (mod(ii,2) == 0) fprintf('ii=%d\n',ii); else fprintf('ii=%d is odd\n',ii); end end % the body has an if elseif statement for ii=1:10 if (ii==10) fprintf('\n'); elseif (mod(ii,3) == 2) fprintf('and '); elseif (mod(ii,3)==1) fprintf('you '); elseif (mod(ii,3)==0) fprintf('me\n'); end end type for4.m % the body has an if elseif statement for ii=1:10 if (ii==10) fprintf('\n'); elseif (mod(ii,3) == 2) fprintf('and '); elseif (mod(ii,3)==1) fprintf('you '); elseif (mod(ii,3)==0) fprintf('me\n'); end end for4 you and me you and me you and me 5. % AVOID DYNAMIC ALLOCATION OF ARRAYS x(1)=1; for k=2:10000 x(k)=k; end % x(1) =1 creates a 1x1 array % for k=10000 THE LINE x(k)=k creates a 1x10000 array from % the previous iteration k=9999 that created a 1x9999 array. % What this means is that you create an array of 10000 elets, % populate them with zeroes, copy the 1x9999 array there and % set the x(k) i.e. x(1,k) value to k. This takes 10000 or 2x10000 % steps (SEE QUIZ 2) % thus the total cost of the array is % 1+ 2 +3 + 4 + ... + 10000 = 10000 x 10001 /2 ~ 50,000,000 % if one had preallocated the array x=zeros(1,10000); % the line x(k)=k would have taken 1 step not k and the total cost % would have been % 1 + 1 + 1 +1 + ... + 1 = 10000 clear type danger.m x(1)=1; for k=2:10000 x(k)=k; end danger whos Name Size Bytes Class Attributes k 1x1 8 double x 1x10000 80000 double sum(1:10000) ans = 50005000 clear; tic for k=2:100000000 x(k)=k; end toc Elapsed time is 17.662734 seconds. <<< EXECUTION TIME %OR INLINE (I.E IN ONE LINE ...) clear tic;x(1)=1; for k=2:100000000 ; x(k)=k; end ; toc Elapsed time is 15.771215 seconds. whos Name Size Bytes Class Attributes k 1x1 8 double x 1x100000000 800000000 double % NOTE THAT AFTER THIS RUN ARRAY x has been created % THERE AFTER THE FOR LOOP IS FAST TO EXECUTE; 1 step per iteration not k! 5. NESTED FOR LOOPS % NESTED FOR LOOPS % THE INNER IS COMPLETED FIRST type nested0.m for ii= 1:1:3 for jj= 1:1:4 fprintf(' ii= %d jj= %d \n',ii,jj); end end nested0 ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 2 jj= 3 ii= 2 jj= 4 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 % A MORE COMPLEX ONE type nested1.m % Code Nest1 FILE: sub7p10nested.m sum =0; for ii= 1:1:3 for jj= 1:1:4 sum = sum + 1; fprintf(' ii= %d jj= %d \n',ii,jj); end end disp(sum); nested1 ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 2 jj= 3 ii= 2 jj= 4 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 12 % Continue ... stop further execution of the current iteration for % the innermost loops get a new value for the variable of the innermost %loop type nested2.m for ii=1:3 for jj=1:4 if (ii==2) && (jj==3) continue; end end fprintf('ii=%3d jj=%3d\n',ii,jj); end nested2 ii= 1 jj= 4 ii= 2 jj= 4 ii= 3 jj= 4 % NOTE THAT THE PRINTF ABOVE IS OUTSIDE OF THE JJ loop type nested2.m for ii=1:3 for jj=1:4 if (ii==2) && (jj==3) continue; end end fprintf('ii=%3d jj=%3d\n',ii,jj); end nested2 ii= 1 jj= 4 ii= 2 jj= 4 ii= 3 jj= 4 % FORMAL INTRODUCTION OF CONTINUE type ccontinue.m for ii=1:10 if (ii==5) continue; end disp(ii); end ccontinue 1 2 3 4 6 7 8 9 10 % ANOTHER EXAMPLE type ccontinue1.m for ii=1:10 disp(ii); if (ii==5) continue; end end ccontinue1 1 2 3 4 5 6 7 8 9 10 type bbreak.m for ii=1:10 if (ii==5) break; end disp(ii); end bbreak 1 2 3 4 type bbreak1. for ii=1:10 disp(ii); if (ii==5) break; end end bbreak1 1 2 3 4 5 type nested3.m for ii=1:3 for jj=1:4 if (ii==2) && (jj==3) continue; end fprintf('ii=%3d jj=%3d\n',ii,jj); end end type nested1.m % Code Nest1 FILE: sub7p10nested.m sum =0; for ii= 1:1:3 for jj= 1:1:4 sum = sum + 1; fprintf(' ii= %d jj= %d \n',ii,jj); end end disp(sum); type nested0.m for ii= 1:1:3 for jj= 1:1:4 fprintf(' ii= %d jj= %d \n',ii,jj); end end nested0 ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 2 jj= 3 ii= 2 jj= 4 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 type nested0.m for ii= 1:1:3 for jj= 1:1:4 fprintf(' ii= %d jj= %d \n',ii,jj); end end type nested0.m for ii= 1:1:3 for jj= 1:1:4 fprintf(' ii= %d jj= %d \n',ii,jj); end end nested0 ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 2 jj= 3 ii= 2 jj= 4 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 type nested3.m for ii=1:3 for jj=1:4 if (ii==2) && (jj==3) continue; end fprintf('ii=%3d jj=%3d\n',ii,jj); end end nested3 ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 2 jj= 4 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 for ii=1:3 for jj=1:4 if (ii==2) && (jj==3) break ; end fprintf('ii=%3d jj=%3d\n',ii,jj); end end nested3a ii= 1 jj= 1 ii= 1 jj= 2 ii= 1 jj= 3 ii= 1 jj= 4 ii= 2 jj= 1 ii= 2 jj= 2 ii= 3 jj= 1 ii= 3 jj= 2 ii= 3 jj= 3 ii= 3 jj= 4 while (0) ; end while (0) disp('hello'); end while (1) type ex1.m limit=4; i=1; while (i < limit) fprintf(' %d\n',i); i=i+1; end type ex1.m limit=4; i=1; while (i < limit) fprintf(' %d\n',i); i=i+1; end type ex2.m limit=3; i=1; while (i <= limit) fprintf(' %d\n',i); i=i+1; end ex1 1 2 3 ex2 1 2 3 type ex1.m limit=4; i=1; while (i < limit) fprintf(' %d\n',i); i=i+1; end type ex3.m type ex3.m limit=3; i=0; while (i < limit) i=i+1; fprintf(' %d\n',i); end ex3 1 2 3 type ex4.m % print only odd numbers limit=10; i=0; while (i < limit) i=i+1; if ( mod(i,2) == 1) fprintf(' %d\n',i); end end for ii = 1:100 ; ssum = sum(1:100); end ; disp(ssum); 5050 for ii = 1:100 ; ssum = ii+1 ; end ; disp(ssum); 101 for ii = 1:100 ; ssum = ii+1 ; end ; disp(ssum); 101 for ii = 1:100 ; ssum = ssum+1 ; end ; disp(ssum); 201 ssum=0; for ii = 1:100 ; ssum = ssum+1 ; end ; disp(ssum); 100 for ii = 1:100 ; ssum = ssum+ii ; end ; disp(ssum); 5150 ssum=0; for ii = 1:100 ; ssum = ssum+ii ; end ; disp(ssum); 5050 type ex1.m limit=4; i=1; while (i < limit) fprintf(' %d\n',i); i=i+1; end type for2syn.m for index = expr % FILE: sub7p7for.m Statement 1 end 3. type for2syn.m for index = expr % FILE: sub7p7for.m Statement 1 end % for loop 10:5:30 generates a row vector of 5 values [10 15 20 25 30] % Each one of them is assigned to variable ii in turn (i.e. 5 assignments). % After each assignment the body of the for loop is executed i.e. the lines % between the for and the end % If there is no any value to be assigned to ii control goes to the statement % after the end. >> for ii=10:5:30 disp(10); disp(ii); end 10 for ii=10:5:30 disp(10); disp(ii); end 10 10 10 15 10 20 10 25 10 30 % AFter the completion of the loop % ii still retains its last assigned value ii ii = 30 % Even if ii =ii *10 multiplies ii by 10 % when ii gets assigned to the new value from [10 15 20 25 30] % the previous values gets overwritten for ii=10:5:30 ; disp(10); disp(ii); ii = ii * 10; end 10 10 10 15 10 20 10 25 10 30 ii % Because the disp precede the ii=ii*10; the same values get printed % Note however that the value of ii after the completion of the % for loop is affected by the ii=ii*10 ii = 300 % CONCLUSION THE TWO PROGRAMS ARE EQUIVALENT IN WHAT THEY DISPLAY... % HOWEVER THE TWO PROGRAMS ARE NOT IDENTICAL. THE STATE OF VARIABLES % IS NOT THE SAME (II is 30 in the former, and 300 in the latter case). % IE THE TWO PROGRAMS HAVE DIFFERENT SIDE EFFECTS (value of ii) % Do not forget that i is such that i*i=-1 . That's why i use ii or jj instead i*i ans = -1