📄 if-eng=case.txt
字号:
为了演示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结构。由于严格单调是一般单调的子集,首先检验严格的单调是必要的,因为在所碰见的第一个真值分支里,其语句执行之后,结构就结束。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -