- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* Application of Matlab Language * 5.2.3 循环结构 循环是指按照给定的条件,重复执行指定的语句,Matlab提供了两种 实现循环结构的语句:for语句和while语句。 1、for语句 for语句的格式为: for 循环变量 表达式1:表达式2:表达式3 循环体语句 end 其中表达式1的值为循环变量的初值,表达式2的值为步长,表达式3的 值为循环变量的终值。步长为1时,表达式2可以省略。 * Application of Matlab Language * 例5.7 已知 ,当n 100时,求y的值。 程序如下: y 0;n 100; for i 1:n y y+1/i/i; end y 输出结果为: y 1.6350 利用Matlab的特点,常用向量运算来代替 循环操作,程序可以如下: n 100; i 1:n; f 1./i.^2; y sum f * Application of Matlab Language * 5.2.3 循环结构 2、while语句 while语句的一般格式为: while条件 循环体语句 end 其执行过程为: 若条件成立,则执行循环体语句,执行后再判断条件是否成立, 如果不成立则跳出循环。 * Application of Matlab Language * 从键盘输入若干个数,当输入0时结束输入,求这些数的平均值和 它们的和。 sum 0; n 0; x input ‘Enter a number end in 0 :’ ; while x~ 0 sum sum+x; n n+1; x input ‘Enter a number end in 0 :’ ; end if n 0 sum mean sum/n end 例5.8 输出结果为: Enter a number end in 0 :67 Enter a number end in 0 :89 Enter a number end in 0 :93 Enter a number end in 0 :70 Enter a number end in 0 :0 sum 319 mean 79.7500 * Application of Matlab Language * 举例 绘制函数z x^2+y^2的曲面 x -4:4;y x; [x,y] meshgrid x,y ; %生成 x-y 坐标“格点”矩阵 z x.^2+y.^2; %计算格点上的函数值 subplot 1,2,1 , mesh x,y,z ; %三维网格图 subplot 1,2,2 , surf x,y,z ; %三维曲面图 colormap hot ; * Application of Matlab Language * 函数z x^2+y^2的曲面的绘制结果 * Application of Matlab Language * 4. 图像文件的读写与图像显示 imread指令 读取图像文件( BMP, GIF , PNG, JPEG, and TIFF) imshow指令 显示图像 imwrite指令 保存图像 例:读取图像文件 img1 imread ‘mudan.jpg ; % Load image data img2 imread eight.tif ; whos img1 img2 * Application of Matlab Language * Name Size Bytes Class img1 750x553x3 1244250 uint8 array img2 242x308 74536 uint8 array 显示图像: imshow img1 ; % Display image * Application of Matlab Language * * Application of Matlab Language * 简单图像处理 lighter 2 * img1; subplot 1,2,1 ; imshow img1 ; title Original ; % Display image subplot 1,2,2 ; imshow lighter ; title ‘Lighter ; % Display image * Application of Matlab Language * 图像处理前后的比较 * Application of Matlab Language * 保存图像 imwrite lighter, mysaved.jpg
文档评论(0)