Control Flow

Control Flow - Chapter 13 Control Flow 13.1 For Loops 'For'...

This preview shows pages 1–5. Sign up to view the full content.

Chapter 13 Control Flow 13.1 For Loops ' For ' Loops are one of several ways to control the order of execution of MATLAB commands in an M-file. It (or something equivalent ) is a basic construct of all high level programming languages. The ' For ' Loop is used when a group of MATLAB commands are to be executed a predetermined number of times. The ' For ' Loop commands are enclosed between the initial line which defines the loop variable and an array of values for it to assume and the ' end ' statement which signals the end of the loop. The general form is for x = array (commands) end Example 13.1.1 A=[1 2; 3 4] % Create square matrix A for i=1:5 % Set array values for loop variable i as 1,2,. ..,5 B=A^i % Compute matrix B from constant matrix A and loop variable i end % for i C=diag(diag(B)) % Create diagonal matrix C using diagonal from matrix B A = 1 2 3 4 B = 1 2 3 4 B = 7 10 15 22 B = 37 54 81 118 B = 199 290 435 634 B = 1069 1558 2337 3406 C = 1069 0 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
0 3406 Example 13.1.2 for x=linspace(0,pi/2,4) % Set array values for loop variable x x y=sin(x) z=cos(x) end % for x x = 0 y = 0 z = 1 x = 0.5236 y = 0.5000 z = 0.8660 x = 1.0472 y = 0.8660 z = 0.5000 x = 1.5708 y = 1 z = 6.1232e-017 The loop variable in a ' For ' Loop is not required to be present in any of the loop commands. If its not, all the loop commands are simply executed for each iteration of the loop. Example 13.1.3 x=0; % Initialize x for k = 1:5 x=x+rand % Accumulate sum end % for k x = 0.9862 x = 1.8716 x = 2.2764 x = 2.9035 x = 3.2890 The loop variable in a ' For ' Loop should not appear by itself on the left side of an assignment statement, i.e. it should not be reassigned because the loop will execute a fixed number of times according to the array of values specified in the first line. Example 13.1.4 y(4)=0 % Preallocate memory for array y for n=1:4 n % Display value of loop variable y(n)=n*rand n=4 % This will not terminate the loop 2
end y = 0 0 0 0 n = 1 y = 0.4712 0 0 0 n = 4 n = 2 y = 0.4712 0.2986 0 0 n = 4 n = 3 y = 0.4712 0.2986 0.4076 0 n = 4 n = 4 y = 0.4712 0.2986 0.4076 2.1300 n = 4 The values assumed by the loop variable are not confined to a specific ordering, e.g. equally spaced numbers. Example 13.1.5 index_values=[1 9 16 25]; for i=index_values i,y=i^0.5 end i = 1 y = 1 i = 9 y = 3 i = 16 y = 4 i = 25 y = 5 The loop variable array values can be based on results from commands issued prior to the loop. For example, it may consist of indices obtained by searching an array for specific conditions. Example 13.1.6 clear all X=10*rand(4) % Create 4 4 array X of 16 random numbers from 0 to 10 indices=[find(X>7)]' % Find indices of X corresponding to elements % greater than 7 and convert to row vector j=1; for i=indices % Set loop index values i,y(j)=X(i) % Save elements of X which are greater than 7 in array y j=j+1; % Increment index j 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
end X = 0.6932 7.3393 0.1289 5.6948 8.5293 5.3652 8.8921 1.5926 1.8033 2.7603 8.6602 5.9436 0.3242 3.6846 2.5425 3.3110 indices = 2 5 10 11 i = 2 y =
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2011 for the course EGM 4320 taught by Professor Klee during the Spring '11 term at University of Central Florida.

Page1 / 17

Control Flow - Chapter 13 Control Flow 13.1 For Loops 'For'...

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

View Full Document
Ask a homework question - tutors are online