文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > Stephen J.Chapman《MATLAB 编程(第二版) 》第四章
侵权投诉

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

end

average1 = (toc)/maxcount; % Calculate average time % Perform calculation with a preallocated array % "square". This calculation is averaged over 10 % loops.

maxcount = 10; % One repetition tic; % Start timer for jj = 1:maxcount clear square % Clear output array square = zeros(1,10000); % Preinitialize array for ii = 1:10000

square(ii) = ii^2; % Calculate square end end

average2 = (toc)/maxcount; % Calculate average time % Perform calculation with vectors. This calculation % averaged over 100 executions. maxcount = 100; % One repetition tic; % Start timer for jj = 1:maxcount clear square % Clear output array ii = 1:10000; % Set up vector square = ii.^2; % Calculate square end

average3 = (toc)/maxcount; % Calculate average time % Display results

fprintf('Loop / uninitialized array = %8.4f\n', average1); fprintf('Loop / initialized array = %8.4f\n', average2); fprintf('Vectorized = %8.4f\n', average3); 4.2.2 break和continue语句

有两个附加语句可以控制while和for循环:break和continuebreak语句可以中止后面的第一句执行,而只中止本次循环,然后返回循环的break语句在循环体中执行,那么体的执行中止,然后执行循环后的第一个可执行性语句。用在for循环中的break语句的例子如下:

程序执行的结果为:

%test_break.m for ii = 1:5; if ii == 3; break; end

fprintf('ii = %d \n', ii); end

disp('End of loop!'); >> test_break ii = 1 ii = 2

End of loop!

注意break语句在ii为3时执行,然后执行

continue语句只中止本次循环,然后返回循环的顶部。在for循环中的控制变量将会更

上一页第13页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email