matlab译文第4章.ppt
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3.After the statements in the body of the loop have been executed, the program assigns the next column of the expression to the loop variable index, and the program executes the statements within the body of the loop again.
Spot the flaw! 4.2 the for loop Executes a block of statements a specified number of times. for index=expr
commands end Index is the loop variable. The loop is executed once for each column in expr.
4. If it is possible to implement a calculation either with a for loop or using vectors, always implement the calculation with vectors, your program will be much faster.
The for loop construct functions as follows.
1.At the beginning of the loop, MATLAB generates the control expression.
2.The first time through the loop, the program assigns the first column of the expression to the loop variable index, and the program executes the statements within the body of the loop.
Quiz 4.1 P162 8,9
Nesting loops P156 Note: inner for loop executes completely before the index variable of the outer for loop is incremented. (seldom use, too slow)
Example 4.5-----Comparing Loops and Vectorization Three ways P152
Using the MATLAB functions tic and toc
wk.baidu.com
The break and continue statements
Break----terminates the execution of a loop and passes control to the next statement after the end of the loop. Continue----terminates the current pass through the loop and returns control to the top of the loop.
Good programming practice: 1.always indent the body of a for loop by 2 or more spaces to improve the readability of the code. 2.never modify the value of a loop index with the body of the loop.
loops
Function: execute a sequence of statements more than once Type: while loops , for loops 4.1 the while loops General form: while expression
commands end
4.Step 3 is repeated over and over as long as there are additional columns in the control expression.
for ii=1:10 commands
end
for ii=1:2:10 commands
end
The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied.
Example 4.1----statistical analysis ex4.4
for ii=[5 9 7] commands
end
for ii=[1 2 3; 4 5 6] commands
end
Quiz4.1 P162 1,2,3,4,5,6,7
Example4.3----calculating the day of year P145
Solution: for loop switch construct
3.always preallocate all arrays used in a loop before executing the loop, this practice greatly increases the execution speed of the loop. The steps of extending an array: 1.Create a new array 2.Copy the contents of the old array to the new longer array 3.Add the new value to the array 4.Delete the old array
Spot the flaw! 4.2 the for loop Executes a block of statements a specified number of times. for index=expr
commands end Index is the loop variable. The loop is executed once for each column in expr.
4. If it is possible to implement a calculation either with a for loop or using vectors, always implement the calculation with vectors, your program will be much faster.
The for loop construct functions as follows.
1.At the beginning of the loop, MATLAB generates the control expression.
2.The first time through the loop, the program assigns the first column of the expression to the loop variable index, and the program executes the statements within the body of the loop.
Quiz 4.1 P162 8,9
Nesting loops P156 Note: inner for loop executes completely before the index variable of the outer for loop is incremented. (seldom use, too slow)
Example 4.5-----Comparing Loops and Vectorization Three ways P152
Using the MATLAB functions tic and toc
wk.baidu.com
The break and continue statements
Break----terminates the execution of a loop and passes control to the next statement after the end of the loop. Continue----terminates the current pass through the loop and returns control to the top of the loop.
Good programming practice: 1.always indent the body of a for loop by 2 or more spaces to improve the readability of the code. 2.never modify the value of a loop index with the body of the loop.
loops
Function: execute a sequence of statements more than once Type: while loops , for loops 4.1 the while loops General form: while expression
commands end
4.Step 3 is repeated over and over as long as there are additional columns in the control expression.
for ii=1:10 commands
end
for ii=1:2:10 commands
end
The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied.
Example 4.1----statistical analysis ex4.4
for ii=[5 9 7] commands
end
for ii=[1 2 3; 4 5 6] commands
end
Quiz4.1 P162 1,2,3,4,5,6,7
Example4.3----calculating the day of year P145
Solution: for loop switch construct
3.always preallocate all arrays used in a loop before executing the loop, this practice greatly increases the execution speed of the loop. The steps of extending an array: 1.Create a new array 2.Copy the contents of the old array to the new longer array 3.Add the new value to the array 4.Delete the old array