你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【安富莱——DSP教程】第5章 Matlab 简易使用(三)

[复制链接]
baiyongbin2009 发布时间:2015-3-14 11:12
本帖最后由 baiyongbin2009 于 2015-3-14 11:14 编辑

特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接
第5章 Matlab 简易使用(三)

    本期教程主要是讲解Matlab的一些编程语句。
    5.1 条件控制——if,else,switch
    5.2 循环控制——for,while,continue,break
    5.3 总结

5.1 控制流5.1.1 条件控制——if,else,switch
    下面我们通过三个简单的例子来说明这三个函数的使用。
l if语句的使用
    % Generate a random number
    a = randi(100, 1);

    % If it is even, divide by 2
    if rem(a, 2) == 0
          disp('a is even')
          b = a/2;
    end
命令窗口输出结果如下:
    >> Untitled2
    a is even
l else语句的使用
    a = randi(100, 1);

    if a < 30
          disp('small')
    elseif a < 80
          disp('medium')
    else
          disp('large')
    end
命令窗口输出结果如下:
        >> Untitled2
      large
l switch语句的使用
[dayNum, dayString] = weekday(date, 'long', 'en_US');

switch dayString
       case 'Monday'
            disp('Start of the work week')
       case 'Tuesday'
            disp('Day 2')
       case 'Wednesday'
            disp('Day 3')
       case 'Thursday'
            disp('Day 4')
       case 'Friday'
            disp('Last day of the work week')
       otherwise
            disp('Weekend!')
    end
命令窗口输出结果如下:
>> Untitled2
Weekend!

在这里顺便介绍一个类似于C语言中scanf的函数input并配合上面的if else实现一个小功能:
yourNumber = input('Enter a number: ');

if yourNumber < 0
        disp('Negative')
elseif yourNumber > 0
        disp('Positive')
else
        disp('Zero')
end
运行上面代码后,我们在命令窗口输入数字22,输出结果如下:
>> Untitled2
    Enter a number: 22
    Positive
5.1.2 循环控制——for, while, continue, break
这里我们也通过几个简单的例子来说明这几个函数的使用。
l for语句的使用
for n = 3:32
       r(n) = rank(magic(n));
end
    r
命令窗口输出结果如下:
>> Untitled2
r =
      Columns 1 through 19
           0     0     3     3     5     5     7     3     9     7    11     3    13     9    15     3    17    11    19
      Columns 20 through 32
           3    21    13    23     3    25    15    27     3    29    17    31     3
l while语句的使用
    a = 0; fa = -Inf;
    b = 3; fb = Inf;
    while b-a > eps*b
         x = (a+b)/2;
         fx = x^3-2*x-5;
         if sign(fx) == sign(fa)
              a = x; fa = fx;
         else
              b = x; fb = fx;
         end
    end
    x
命令窗口输出结果如下:
>> Untitled2
    x =
          2.0946
l continue语句的使用
     fid = fopen('magic.m','r');
    count = 0;
    while ~feof(fid)
          line = fgetl(fid);
           if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
                continue
          end
          count = count + 1;
    end
    fprintf('%d lines\n',count);
    fclose(fid);
命令窗口的输出结果如下:
>> Untitled2
    31 lines
l break语句的使用
    a = 0; fa = -Inf;
    b = 3; fb = Inf;
    while b-a > eps*b
          x = (a+b)/2;
          fx = x^3-2*x-5;
          if fx == 0
               break
          elseif sign(fx) == sign(fa)
              a = x; fa = fx;
         else
              b = x; fb = fx;
         end
    end
    x
命令窗口输出结果如下:
>> Untitled2
    x =
            2.0946
5.1.3 矢量化
    对于matlab而言,要想加快算法的执行速度可以通过算法的矢量化来实现,比如要实现如下的功能。
x = .01;
for k = 1:1001
       y(k) = log10(x);
       x = x + .01;
end
但是我们矢量化后,将更加方便和容易实现。
x = .01:.01:10;
y = log10(x);
但是有一点大家要特别注意,不是什么程序矢量化都能加快执行速度,要视具体情况而定。
5.2 Matlab中help的使用
    关于matlab入门方面的东西就跟大家将这么多,基本上有这些基础就够了,后面遇到什么问题在网上查找资料即可。也可以查看matlab本身的help帮助文档。点击这里就可以查看。
5.1.png
如果有不懂的函数,可以直接在命令窗口输入help 再加上函数即可,比如输入:
5.2.png
5.3 总结
    Matlab方面的教程就跟大家讲这么多,后面需要那方面知识的时候,我们再具体的补充。基本上面学会这些基本的操作就可以入门了。永远要记住,Matlab只是个工具,我们只需把它当个工具来用,没有必要花大量的时间去系统的学习,入门后用什么学什么即可。

收藏 评论3 发布时间:2015-3-14 11:12

举报

3个回答
kqh1120 回答时间:2015-3-14 11:34:51
谢谢分享 14.gif
bit 回答时间:2015-3-14 16:44:44
帮顶帮顶
_ilikerome_ 回答时间:2015-3-15 08:53:35
谢谢分享啊  

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版