?? if-eng=case.txt
字號:
為了演示If-Else-End結構,考慮精通MATLAB工具箱中函數(shù)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工具箱的函數(shù)主體給出如下:
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
函數(shù)mmono直接利用了If-Else-End結構。由于嚴格單調是一般單調的子集,首先檢驗嚴格的單調是必要的,因為在所碰見的第一個真值分支里,其語句執(zhí)行之后,結構就結束。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -