MATLAB学习第四天(决策语句)

简介: MATLAB学习第四天(决策语句)

MATLAB决策制定

本节内容:了解MATLAB提供的决策类型,及使用它们进行决策制定。

决策结构用来做什么?决策结构要求程序员能够使用一个或者多个的条件来对程序进行评估或者测试,沿着一条或多条语句执行,如果该条件被确定为真,则继续执行;如果条件确定是假的(false),执行其他要执行的语句。

下图是一个典型的决策结构,是大多数编程语言的一般形式:

68dffaf2ecc6c29a73574cec3a23e075_3b36474fdaf942fe07f9f749465b8bc6.png

MATLAB 提供的决策类型如下表:


语句 描述
if ... end statement 一个 if ... end 语句由一个布尔表达式组成,后跟一个或多个语句。
if...else...end statement 一个 if 语句可以跟随一个可选的 else 语句,当布尔表达式为false时,该语句将执行。
If... elseif...elseif...else...end statements 一个 if 语句后面可以有一个(或多个)可选 elseif ... 和一个 else 语句,这对于测试各种条件非常有用。
nested if statements 你可以在另一个 if 或 elseif 语句中使用一个 if 或 elseif 语句。
switch statement 一个 switch 语句允许根据值列表对变量进行相等的测试。
nested switch statements 你可以在另一个 switch 语句中使用一个 swicth 语句。


  • MATLAB if...end 语句

一个 if 语句和一个布尔表达式后跟一个或多个语句,由 end 语句分隔,就是一个 if ... end 语句


MATLAB if 语句语法


在MATLAB中 的 if 语句的语法是:

if <expression>
% statement(s) will execute if the boolean expression is true 
<statements>
end


表达式的计算结果如果是“true”,那么在代码块中,如果语句会被执行。如果表达式计算结果为“false”,那么第一套代码结束后的语句会被执行。


MATLAB if 语句流程图:


3e900080429fe0f839bb4bb63edc1273_7dcd97fa78020aa19be4e02870215654.png

详细例子如下:


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

a = 10;
% check the condition using if statement 
   if a < 20 
   % if condition is true then print the following 
       fprintf('a is less than 20
' );
   end
fprintf('value of a is : %d
', a);


运行该文件,显示下述结果:

a is less than 20
value of a is : 10


  • MATLAB if...else...end 语句

在MATLAB的 if...else...end 语句中,if 语句后面可以跟一个可选择的 else 语句,当执行的表达式为假的时候,执行 else 语句。


if...else...end 语句语法:


MATLAB 中一个 if ... else 语句的语法示例:

if <expression>
% statement(s) will execute if the boolean expression is true 
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the boolean expression is false 
end


如果布尔表达式的值为 “true”,那么执行 if 的代码块;如果布尔表达式的值为 “false”,else 的代码块将被执行。


if...else...end 语句流程图:


6190ddf705bc8c8a16d5aca3adcb5427_0916ab8a4c1de7637ce8f98a47f0c17a.png

详细例子如下:


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

a = 100;
% check the boolean condition 
   if a < 20 
        % if condition is true then print the following 
       fprintf('a is less than 20
' );
   else
       % if condition is false then print the following 
       fprintf('a is not less than 20
' );
   end
   fprintf('value of a is : %d
', a);


编译和执行上述代码,产生下述结果:

a is not less than 20
value of a is : 100


  • MATLAB if...elseif...elseif...else...end 语句

MATLAB 的 if...elseif...elseif...else...end 语句中 if 语句可以跟随一个(或多个)可选的 elseif... else 语句,这是非常有用的,可以用来对各种条件进行测试。


使用 if... elseif...elseif...else 语句,要注意以下几点:


一个 if 可以有零个或多个 else,但是它必须跟在 elseif 后面(即只有 elseif 存在才会有 else)。


一个 if 可以有零个或多个 elseif ,必须出现else。


一旦 elseif 匹配成功,余下的 elseif 将不会被测试。


if... elseif...else...end 语法:

if <expression 1>
% Executes when the expression 1 is true 
<statement(s)>
elseif <expression 2>
% Executes when the boolean expression 2 is true
<statement(s)>
Elseif <expression 3>
% Executes when the boolean expression 3 is true 
<statement(s)>
else 
%  executes when the none of the above condition is true 
<statement(s)>
end


详细例子如下:


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

a = 100;
%check the boolean condition 
   if a == 10 
         % if condition is true then print the following 
       fprintf('Value of a is 10
' );
    elseif( a == 20 )
       % if else if condition is true 
       fprintf('Value of a is 20
' );
    elseif a == 30 
        % if else if condition is true  
       fprintf('Value of a is 30
' );
   else
        % if none of the conditions is true '
       fprintf('None of the values are matching
');
   fprintf('Exact value of a is: %d
', a );
   end

编译和执行上述代码,产生如下结果:

None of the values are matching
Exact value of a is: 100


  • MATLAB嵌套if语句

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


MATLAB嵌套 if 语句语法:


详细语法如下:

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

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


详细例子如下:


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

a = 100;
b = 200;
    % check the boolean condition 
   if( a == 100 )
       % if condition is true then check the following 
       if( b == 200 )
          % if condition is true then print the following 
          fprintf('Value of a is 100 and b is 200
' );
       end
   end
   fprintf('Exact value of a is : %d
', a );
   fprintf('Exact value of b is : %d
', 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 <switch_expression>
   case <case_expression>
     <statements>
   case <case_expression>
     <statements>
     ...
     ...
   otherwise
       <statements>
end


详细例子


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

n = input('Enter a number: ');
switch n
    case -1
        disp('negative one')
    case 0
        disp('zero')
    case 1
        disp('positive one')
    otherwise
        disp('other value')
end


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

positive one

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

other value


  • MATLAB 嵌套switch语句

在 MATLAB 中嵌套 switch 语句是可能的,可以在 switch 一部分外嵌套 switch 语句序列。即使 case 常量的内部和外部的 switch 含有共同的值,也不算冲突出现。


MATLAB嵌套switch语句语法


嵌套switch语句的语法如下:

switch(ch1) 
   case 'A' 
   fprintf('This A is part of outer switch');
      switch(ch2) 
         case 'A'
           fprintf('This A is part of inner switch' );
          case 'B'  
          fprintf('This B is part of inner switch' );
       end   
case 'B'
fprintf('This B is part of outer switch' );
end


详细例子:


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

a = 100;
b = 200;
switch(a) 
      case 100 
         fprintf('This is part of outer switch %d
', a );
         switch(b) 
            case 200
               fprintf('This is part of inner switch %d
', b );
         end
end
fprintf('Exact value of a is : %d
', a );
fprintf('Exact value of b is : %d
', b );


当运行该文件时,它会显示:

This is part of outer switch 100
This is part of inner switch 200
Exact value of a is : 100
Exact value of b is : 200

目录
相关文章
OFDM深入学习及MATLAB仿真(二)
OFDM深入学习及MATLAB仿真
1092 1
|
编解码 网络协议 前端开发
OFDM深入学习及MATLAB仿真(一)
OFDM深入学习及MATLAB仿真
2985 1
|
索引
matlab日常学习-------矩阵
matlab日常学习-------矩阵
|
机器学习/深度学习 算法 数据挖掘
没发论文的注意啦!重磅更新!GWO-BP-AdaBoost预测!灰狼优化、人工神经网络与AdaBoost集成学习算法预测研究(Matlab代码实现)
没发论文的注意啦!重磅更新!GWO-BP-AdaBoost预测!灰狼优化、人工神经网络与AdaBoost集成学习算法预测研究(Matlab代码实现)
452 0
|
机器学习/深度学习 运维 算法
【微电网多目标优化调度】多目标学习者行为优化算法MOLPB求解微电网多目标优化调度研究(Matlab代码实现)
【微电网多目标优化调度】多目标学习者行为优化算法MOLPB求解微电网多目标优化调度研究(Matlab代码实现)
490 1
|
机器学习/深度学习 算法 数据可视化
【一共21份资源】【入门学习】【打包带走】时序+回归+预测入门学习(Matlab代码实现)
【一共21份资源】【入门学习】【打包带走】时序+回归+预测入门学习(Matlab代码实现)
190 3
|
机器学习/深度学习 算法 新能源
基于动态非合作博弈的大规模电动汽车实时优化调度电动汽车决策研究(Matlab代码实现)
基于动态非合作博弈的大规模电动汽车实时优化调度电动汽车决策研究(Matlab代码实现)
317 0
|
数据采集 机器学习/深度学习 存储
关于在核特征空间中学习POD基础下Koopman算子的稀疏表示(Matlab代码实现)
关于在核特征空间中学习POD基础下Koopman算子的稀疏表示(Matlab代码实现)
381 1
|
机器学习/深度学习 算法 Python
【分布鲁棒】基于Wasserstein距离的两阶段分布鲁棒简易模型【对偶转化】【线性决策】(Matlab代码实现)
【分布鲁棒】基于Wasserstein距离的两阶段分布鲁棒简易模型【对偶转化】【线性决策】(Matlab代码实现)
796 0
|
机器学习/深度学习 传感器 边缘计算
Koopman-MPC: 基于数据驱动的学习和控制四旋翼无人机研究(Matlab代码实现)
Koopman-MPC: 基于数据驱动的学习和控制四旋翼无人机研究(Matlab代码实现)
542 0

热门文章

最新文章