matlab programming-4.ppt_第1页
matlab programming-4.ppt_第2页
matlab programming-4.ppt_第3页
matlab programming-4.ppt_第4页
matlab programming-4.ppt_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、,4 Loops,王素珍 青岛理工大学 : Mobile Tel:132-1002-1986,4.1 The While Loop,A while loop is a block of statements that is repeated indefinitely as long as some condition is satisfied. The general form of a while loop is while expression end If the expression is non-zero (true), the code block will be executed

2、, and then control will return to the while statement. If the expression is still non-zero (true), the statements will be executed again.,4.1 The While Loop,The pseudocode corresponding to a while loop is While expre end Example 4.1,(4.1),(4.2),4.2 The for Loop,The for loop is a loop that executes a

3、 block of statements a specified number of times. The for loop has the form for index=expr statement 1 statement n end Where index is the loop variable ( also known as the loop index) and expr is the loop control expression. The expression usually takes the forms of a vector on short notation first:

4、incr:last.,Body,4.2 The for Loop,An example: In this case, the control expression generated a 110 array, so statements 1 through n will be executed 10 times.,for ii=1:10 statement 1 statement n end,4.2 The for Loop,An example: In this case, the control expression generated a 15 array, so statements

5、1 through n will be executed 5 times.,for ii=1:2:10 statement 1 statement n end,4.2 The for Loop,An example: Here, the control expression is an explicity written 13 array, so statements 1 through n will be executed 3 times with the loop index set to 5 the first time, 9 the second time, and 7 the fin

6、al time. The loop index ii will set to 7 after the loop finished executing.,for ii=5 9 7 statement 1 statement n end,4.2 The for Loop,An example: In this case, the control expression generated a 23 array, so statements 1 through n will be executed 3 times. The loop index ii will be the column vector

7、 1;4 the first time, 2;5 the second time, and 3;6 the third time. This example illustrates the fact that a loop index can be a vector.,for ii=1 2 3; 4 5 6 statement 1 statement n end,4.2 The for Loop,Example 4.2 Example 4.3 Example 4.4,4.2.1 Details of Operation,Always indent the body of a for loop

8、by 2 or more spaces to improve the readability of the code Never modify the value of a loop index within the body of the loop. Always pre-allocate all arrays used in a loop before executing the loop. This practice greatly increases the execution speed of the loop. If it is possible to implement a ca

9、lculation either with a for loop or using vectors, always implement the calculation with vectors. Your program will be much faster. Example 4.5,4.2.2 the break and continue statements,There are two additional statements that can be used to control the operation of while loops and for loops: the brea

10、k and continue statements. The break statement terminates the execution of a loop and passes control to the next statement after the end of the loop, while the continue statement terminates the current pass through the loop and returns control to the top of the loop.,4.2.2 the break and continue sta

11、tements,An example for ii=1:5 if ii= =3 ; break; end fprintf ( ii= %dn , ii) end disp (end of loop!) When this program is executed, the output is test_break ii=1 ii=2 end of loop!,4.2.2 the break and continue statements,If a continue statement is executed in the body of a loop. The execution of the

12、current pass through the loop will stop and control will return to the top of the loop. The controlling variable in the for loop will take on its next value, and the loop will be executed again. an example:,4.2.2 the break and continue statements,an example: While this program is executed, the outpu

13、t is,for ii=1:5 if ii= =3 ; continue; end fprintf ( ii= %dn , ii) end disp (end of loop!), test_continue ii=1 ii=2 ii=4 ii=5 end of loop!,4.2.3 Nesting Loops,It is possible for one loop to be completely inside another loop. If one loop is completely inside another one, the two loops are called neste

14、d loops. An example:,for ii=1:3 for jj=1:3 product=ii*jj; fprintf ( %d* %d =%dn , ii,jj, product) end end,4.2.3 Nesting Loops,The resulting output is Note that the inner for loop executes completely before the index variable of the outer for loop is incremented.,1*1=1 1*2=2 1*3=3 2*1=2 2*2=4 2*3=6 3

15、*1=1 3*2=6 3*3=9,4.2.3 Nesting Loops,If a break or continue statement appears inside a set of nested loops, then the break statement refers to the innermost of the loops containing it. An example:,for ii=1:3 for jj=1:3 if jj=3; break; end product=ii*jj; fprintf ( %d* %d= %dn , ii, jj, product); end

16、fprinf ( end of inner loopn ) end fprintf ( end of outer loopn );,4.2.3 Nesting Loops,The resulting output values are,1*1=1 1*2=2 End of inner loop 2*1=2 2*2=4 End of inner loop 3*1=3 3*2=6 End of inner loop End of outer loop,4.3 Logical Arrays and Vectorization,Logical arrays are standard numeric a

17、rrays with a special “logical” property added and are created by all relational and logical operators. They can be distinguished from standard arrays because the (logical) modifier appears after them in the whos command or the Workspace Browser. For example, consider the following statement: a=1 2 3

18、; 4 5 6; 7 8 9; b=a5; then b=0 0 0; 0 0 1; 1 1 1,4.3 Logical Arrays and Vectorization,When the whos command is executed, the results are as shown below. Note that array b has the (logical) modifier applied to it. whos Name Size Bytes Class a 33 72 double array b 33 72 double array (logical) Grand to

19、tal is 18 elements using 144 bytes.,4.3 Logical Arrays and Vectorization,It is also possible to add the logical property to an array by using the logical function. For example, the statement c=logical (a) will assign the values in array a to array c, while setting the logical property for the array.

20、 c=logical (a) C=1 2 3; 4 5 6; 7 8 9 whos Name Size Bytes Class a 33 72 double array b 33 72 double array (logical) b 33 72 double array (logical) Grand total is 27 elements using 216 bytes.,4.3 Logical Arrays and Vectorization,The logical property can be removed from an array by performing almost a

21、ny arithmetic operation on it. For example, if we add zero to array c, the values in the array will be unchanged, but the logical property will be lost. c=c+0 c=1 2 3; 4 5 6; 7 8 9 whos Name Size Bytes Class a 33 72 double array b 33 72 double array (logical) b 33 72 double array Grand total is 27 e

22、lements using 216 bytes.,4.3.1 The Significance of Logical Arrays,A mask is an array that selects the elements of another arrays for use in an operation. The specified operation will be applied to the selected elements, but not to the remaining elements. a(b)=sqrt(a(b) a= 1.0000 2.0000 3.0000 4.0000

23、 5.0000 2.4495 2.6458 2.8284 3.0000 This is a very fast and very clever way of performing an operation on a subset of an array without needing loops and branches.,Take the square root of all elements for which the logical array b is nonzero and leave all the other elements in the array unchanged.,4.3.1 The Significance of Logical Arrays,The following two code fragments bot

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论