Matlab分支和循环讲述.ppt

  1. 1、本文档共49页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* Branches (try/catch) Example * 循环语句 An Overview 在实际问题中,有时需要有规律的重复运算,相应的程序中就需要反复的执行某些语句。一组被重复执行的语句称为循环体。每循环一次,都要进行判断,判断的依据称为循环的终止条件。 for语句循环的特点是判断条件通常是对循环次数的判断,一般循环次数预先设定好;而while语句的控制条件是一个逻辑判断语句,因此,它的适用范围更广一些。 * 循环语句 Loops: a) while-loop Statement * Loops (while-loop) The while loop statement syntax is a block of statements is repeated as long as some condition expression is satisfied. If the expression is non-zero(true),the code block will be executed, and then control will return to the while statement. The process will be repeated until the expression become zero(false). * 循环语句 Loops: b) for-loop Statement * Loops (for-loop) The for loop statement syntax is a loop that executes a block of statements a specified number of times. For-loop has the form as follows: Where the index is the loop variable and expr is the loop control expression, which usually has the form of a vector in notation first:incr:last. The statements between the for statement and the end statement are known as the body of the loop. * Loops (for-loop) Some specific examples Control expression is a 1×10 array. Control expression is a 1×5 array. Control expression is a 1×3 array. Control expression is a 2×3 array. * Example Write a program to find the index of the maximum value in a matrix. First, write your program with a for…end loop Second, write your program with a while…end loop * Solution: For-Loop [m,n] = size(A); max_val = A(1,1); for r = 1:m for c = 1:n if A(r,c) max_val max_row = r; max_col = c; max_val = A(r,c); end end end * Solution: While-Loop r = 1; [m, n] = size(A); max_val = A(1,1); while r = m c = 1; while c = n if A(r,c) max_val max_row = r; max_col = c; max_val = A(r,c); end c = c+1; end r = r+1; end * Matlab的程序流控制 综上, Matlab的控制语句同c语言有相似之处, 但没有c 语言复杂、灵活和多变。因而语法比较简单,容易掌握。 echo 命令(用来控制m文件在执行过程中是否 显示) echo on — 打开所有命令文件的显示方式

文档评论(0)

整理王 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档