首页 > 其他 > 详细

MATLAB快速入门

时间:2015-11-03 00:55:09      阅读:389      评论:0      收藏:0      [点我收藏+]

前言

        MATLAB将数值分析、矩阵运算、科学数据可视化等诸多强大的功能集成在一个易于使用的视窗环境中,被广泛应用于数据挖掘、工程与科学绘图、控制系统设计与仿真、图像处理、数字信号处理、财务与金融工程、计量经济学分析等领域。本篇主要通过一些简单地例子,讲解MATLAB的快速入门。

程序结构

1、循环for语句

>> s=1;
>> for i=1:10
    s=s+i;    % MATLAB是解释性语言,语句末尾不加‘;‘会导致执行1句显示1次结果
end
>> fprintf(‘s=%f\n‘,s);    % 语法与C语言相似,格式化输出,注意是‘符号
s=56.000000
>>

2、循环while语句

>> i=1;
>> s=0;
>> while i<=10
    s=s+i;
    i=i+1;
end
>> fprintf(‘ans=%d\n‘,s);

3、分支if语句

>> 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

4、分支switch语句

 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

5、异常try-catch语句

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

6、循环控制continue语句

>> for i=1:10
    if mod(i,2)==1    % MATLAB求余语句
        continue;
    end
    fprintf(‘%d\n‘,i);
end

7、循环控制break语句

>> i=1;
>> while true
    if i>10
        break;
    end
    i=i*2;
    fprintf(‘%d\n‘,i);
end

未完待续... ...

MATLAB快速入门

原文:http://my.oschina.net/keyven/blog/525073

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!