MATLAB将数值分析、矩阵运算、科学数据可视化等诸多强大的功能集成在一个易于使用的视窗环境中,被广泛应用于数据挖掘、工程与科学绘图、控制系统设计与仿真、图像处理、数字信号处理、财务与金融工程、计量经济学分析等领域。本篇主要通过一些简单地例子,讲解MATLAB的快速入门。
>> s=1; >> for i=1:10 s=s+i; % MATLAB是解释性语言,语句末尾不加‘;‘会导致执行1句显示1次结果 end >> fprintf(‘s=%f\n‘,s); % 语法与C语言相似,格式化输出,注意是‘符号 s=56.000000 >>
>> i=1; >> s=0; >> while i<=10 s=s+i; i=i+1; end >> fprintf(‘ans=%d\n‘,s);
>> x=input(‘Please enter the value of ‘‘x‘‘, an Integer number between 0 and 9:‘); Please enter the value of ‘x‘, an Integer number between 0 and 9:0 >> if x==0||x==9 fprintf(‘x=0 or x=9\n‘); elseif 1<=x&&x<=5 fprintf(‘1<=x<=5\n‘); else fprintf(‘6<=x<=8\n‘); end x=0 or x=9
x = input(‘enter an Integer value or a String:‘); enter an Integer value or a String:‘Hello‘ >> switch x case ‘Goodbye!‘ fprintf(‘Bye!\n‘); case {‘Hello‘,‘Hello!‘} fprintf(‘Hi!\n‘); case 0 fprintf(‘Zero\n‘); case {1,2} fprintf(‘one or tow\n‘); otherwise fprintf(‘I don‘‘t know!\n‘); end
try x = [1,2;3,4]; % 2x2数组 y = [5,6,7;1,2,3;4,5,6]; % 3x3数组 z = x * y; catch errordlg(‘矩阵相乘错误!‘,‘Error‘); end
>> for i=1:10 if mod(i,2)==1 % MATLAB求余语句 continue; end fprintf(‘%d\n‘,i); end
>> i=1; >> while true if i>10 break; end i=i*2; fprintf(‘%d\n‘,i); end
未完待续... ...
原文:http://my.oschina.net/keyven/blog/525073