• MATLAB嵌套if语句||MATLAB switch语句


    MATLAB嵌套if语句

    在MATLAB中嵌套if语句始终是合法的,也就是说可以使用一个嵌套的 if-else语句 if 或 elseif 语句在另一个 if 或 elseif 语句。

    MATLAB嵌套 if 语句语法:

    详细语法如下:

    if 
    % Executes when the boolean expression 1 is true 
       if 
          % Executes when the boolean expression 2 is true    
      end
    end

    可以嵌套 elseif 或其他类似的方式,因为已经嵌套 if 语句。

    详细例子如下:

    在MATLAB中建立一个脚本文件,并输入下面的代码:

    1. a = 100;
    2. b = 200;
    3. % check the boolean condition
    4. if( a == 100 )
    5. % if condition is true then check the following
    6. if( b == 200 )
    7. % if condition is true then print the following
    8. fprintf('Value of a is 100 and b is 200
    9. ' );
    10. end
    11. end
    12. fprintf('Exact value of a is : %d
    13. ', a );
    14. fprintf('Exact value of b is : %d
    15. ', b );

    运行该文件,它显示的结果如下:

    Value of a is 100 and b is 200
    Exact value of a is : 100
    Exact value of b is : 200

    MATLAB switch语句

    MATLAB中 switch 块有条件地执行一组语句,这些语句是从几个选项里选择执行的,其中每个选项涵盖了一个 case 语句。

    请记住:

    • 计算 switch_expression 是一个标量或字符串。
    • 计算 case_expression 是标量,标量或字符串的字符串或单元阵列。

    switch 块的功能是测试每个 case ,直到被测试的其中一个 case 是 true 。

    case 是 true 的情况如下:

    • 对于数字,eq(case_expression,switch_expression).

    • 对于字符串,strcmp(case_expression,switch_expression).

    • 对于对象,支持 eq 函数,eq(case_expression,switch_expression).

    • 对于单元阵列case_expression的,在单元阵列与switch_expression相匹配的元素中的至少一个,如上文所定义的数字,字符串和对象。

    当上述有一个情况是 true,MATLAB 就执行与之相应的语句,然后不再执行以后的语句,直接退出 switch 块。

    otherwise 块是可选的,任何情况下,只有当真正执行。

    MATLAB switch语句语法

    在MATLAB 中 switch 语句的语法如下:

    switch 
       case 
         
       case 
         
         ...
         ...
       otherwise
           
    end

    详细例子

    在MATLAB中建立一个脚本文件,并输入下述代码:

    1. n = input('Enter a number: ');
    2. switch n
    3. case -1
    4. disp('negative one')
    5. case 0
    6. disp('zero')
    7. case 1
    8. disp('positive one')
    9. otherwise
    10. disp('other value')
    11. end

    在命令提示符下,输入数字 1,输出结果为:

    positive one

    重复执行该代码并输入数字 3,输出结果为:

    other value
  • 相关阅读:
    什么是R-tree?
    目标检测YOLO实战应用案例100讲-基于深度学习的可见光遥感图像目标检测(下)
    Nreal for Unity SDK 发布安卓参数设置
    【计算机网络】——数据链路层(应用:介质访问控制)
    Supervisor监控Go程序
    3个iPhone设置让你成为“人类高质量iPhone用户”
    RTMP服务器搭建
    猿创征文|一位测试小伙伴面试笔试题的救场 - 这是一篇枪手角度的答题文(最后小伙伴吹大了,痛失offer)
    集成学习算法
    容联七陌助力鱼跃医疗升级智能联络中心,让客户服务更“鱼跃”
  • 原文地址:https://blog.csdn.net/m0_69824302/article/details/134495386