if, elseif, else
条件为 true 时执行语句
语法
ifexpression
statements
elseifexpression
statements
elsestatements
end
说明
示例
使用 if、elseif 和 else 指定条件
创建一个由 1 组成的矩阵。
nrows = 4; ncols = 6; A = ones(nrows,ncols);
遍历矩阵并为每个元素指定一个新值。对主对角线赋值 2
,对相邻对角线赋值 -1
,对其他位置赋值 0
。
for c = 1:ncols for r = 1:nrows if r == c A(r,c) = 2; elseif abs(r-c) == 1 A(r,c) = -1; else A(r,c) = 0; end end end A
A = 4×6
2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
比较数组
在数组中包含关系运算符的表达式(例如 A > 0
)仅在结果中的每个元素都为非零时才为 true。
使用 any
函数测试任何结果是否为 true。
limit = 0.75; A = rand(10,1)
A = 10×1
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
0.9575
0.9649
if any(A > limit) disp('There is at least one value above the limit.') else disp('All values are below the limit.') end
There is at least one value above the limit.
测试数组的相等性
使用 isequal
而不是 ==
运算符比较数组来测试相等性,因为当数组的大小不同时 ==
会导致错误。
创建两个数组。
A = ones(2,3); B = rand(3,4,5);
如果 size(A)
与 size(B)
相同,则会串联这两个数组;否则显示一条警告并返回一个空数组。
if isequal(size(A),size(B)) C = [A; B]; else disp('A and B are not the same size.') C = []; end
A and B are not the same size.
比较字符向量
使用 strcmp
比较字符向量。当字符向量的大小不同时,使用 ==
测试相等性会产生错误。
reply = input('Would you like to see an echo? (y/n): ','s'); if strcmp(reply,'y') disp(reply) end
测试值的不相等性
评估表达式中的多个条件
确定值是否在指定范围内。
x = 10; minVal = 2; maxVal = 6; if (x >= minVal) && (x <= maxVal) disp('Value within specified range.') elseif (x > maxVal) disp('Value exceeds maximum value.') else disp('Value is below minimum value.') end
Value exceeds maximum value.
详细信息
表达式
表达式可以包含关系运算符(例如 <
或 ==
)和逻辑运算符(例如 &&
、||
或 ~
)。使用逻辑运算符 and
和 or
创建复合表达式。MATLAB® 按照运算符优先级规则从左至右计算复合表达式。
在 if...end
块的条件表达式内,逻辑运算符 &
和 |
的行为与短路运算符的行为相同。此行为分别相当于 &&
和 ||
。由于 &&
和 ||
在条件表达式和语句中一致短路,因此,建议在该表达式中使用 &&
和 ||
,而不是 &
和 |
。例如,
x = 42; if exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') end
表达式的第一部分的计算结果为 false。因此,MATLAB 不需要计算表达式的第二部分,否则会导致未定义的函数错误。
提示
您可以嵌套任意数量的
if
语句。每个if
语句需要一个end
关键字。避免在
elseif
关键字 (else if
) 内于else
之后添加空格。空格会创建嵌套的if
语句,该语句要求独立的end
关键字。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
HDL 代码生成
使用 HDL Coder™ 为 FPGA 和 ASIC 设计生成 Verilog 代码和 VHDL 代码。
不要在
if
语句的条件中使用&
和|
运算符。在这种情况下,请使用&&
和||
运算符。HDL Coder™ 不支持在
if
语句的条件中使用非标量表达式。在这种情况下,请使用all
或any
函数将逻辑向量折叠成标量。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅Run MATLAB Functions in Thread-Based Environment。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)