网站首页  |  幼儿教育  |  中小学教育  |  电脑教育  |  英语教育  |  教育论文  |  家长教育 设为首页加入收藏联系投稿 
 位置: 中国教育学习网 > 电脑教育 > 机械电子 > MATLAB > 正文

M文件举例

字号:   

    为了演示If-Else-End结构,考虑精通MATLAB工具箱中函数mmono,它检查一个向量的单调性。

                   » mmono(1:12)     %  strictly increasing input

                   ans =

                        2

                   » mmono([1:12  12  13:24])     %  non decreasing input

                   ans =

                        1

                   » mmono([1  3  2  -1])     %  not monotonic in any sense

                   ans =

                        0

                   » mmono([12:-1:0  0  -1])     %  non increasing

                   ans =

                       -1

                   » mmono(12:-1:0)     %  strictly decrasing

                   ans =

                       -2

这个精通MATLAB工具箱的函数主体给出如下:

          function f=mmono(x)

          %  MMONO Test for monotonic vector.

          %  MMONO(x) where x is a vector return:

          %    2 if x is strictly increasing,

          %    1 if x is non decreasing,

          %   -1 if x is non increasing,

          %   -2 if x is strictly decreasing,

          %    0 otherwise.

          %  Copyright (c) 1996 by Prentice-Hall,Inc.

          x=x(:);        %  make x a column vector

          y=diff(x);      %  find differences between consecutive elements

          if all(y>0)      %  test for strict first

                     f=2;

          elseif all(y>=0)

                     f=1;

          elseif all(y<0)   %  test for strict first

                     f=-2;

          elseif all(y<=0) 

                     f=-1;

          else

                     f=0;   %  otherwise response

          end

    函数mmono直接利用了If-Else-End结构。由于严格单调是一般单调的子集,首先检验严格的单调是必要的,因为在所碰见的第一个真值分支里,其语句执行之后,结构就结束。

收藏此文  |  打印  

 
  • 上一篇教程:

  • 下一篇教程:

  •   GoogLe
     
      最新推荐
  • 此栏目下没有推荐教程

  •   最近更新

      GoogLe

     
    Powered by Cn-Education.Com (c) 2005-2008 中国教育学习网 教育网站长QQ交流群60041790
    设为首页  |  加入收藏  |  版权申明  |  广告服务  |  联系我们  |  友情链接  |  网站地图  |  返回顶部 ↑