16-2 ������������

MATLAB 䴩GرO]Branching CommandAΤĶuOv^A@جO if-then-else OAt@جO MATLAB bĤ}l䴩 switch - case - otherwise OAHUzC

̱`ΪOO if - then - elseAϥλykG

if B⦡@; else B⦡G; end

󦡦߮ɡAMATLAB NB⦡@A_hANB⦡GCYݨϥιB⦡GAhiٲ else MB⦡GCҦpGbƭȹB⪺L{AYܼƭȬ NaN]Y Not A Number ^ɡAڭ̭nߨLXĵiTAiJpUG

Example 1: 16-{y{/if01.mx = 0/0; if isnan(x) disp('Warning: NaN detected!'); endWarning: NaN detected!

bWҤAisnan(x) iΩP_ x O_ NaNAYOAhǦ^ 1]u^A_hYǦ^ 0]^C

bUҤAڭ̥iھڦVq y Ȭ_ƩΰơAܤPTG

Example 2: 16-{y{/if02.my = [0 3 4 1 6]; for i = 1:length(y) if rem(y(i), 2)==0 fprintf('y(%g) = %g is even.\n', i, y(i)); else fprintf('y(%g) = %g is odd.\n', i, y(i)); end endy(1) = 0 is even. y(2) = 3 is odd. y(3) = 4 is even. y(4) = 1 is odd. y(5) = 6 is even.

Wz if - then - else VAY{u|uB⦡@vΡuB⦡ GvA|ĤTإiC

MATLAB ihVAϥλykG

if 󦡤@ B⦡@; elseif 󦡤G B⦡G; elseif 󦡤T B⦡T; else B⦡|; end

iYnihVAuݤ@A elseif YiCҦpAP_ y VqOݩ 3nB3n1B 3n+2AiJpUG

Example 3: 16-{y{/if03.my = [3 4 5 9 2]; for i = 1:length(y) if rem(y(i),3)==0 fprintf('y(%g)=%g is 3n.\n', i, y(i)); elseif rem(y(i), 3)==1 fprintf('y(%g)=%g is 3n+1.\n', i , y(i)); else fprintf('y(%g)=%g is 3n+2.\n', i , y(i)); end endy(1)=3 is 3n. y(2)=4 is 3n+1. y(3)=5 is 3n+2. y(4)=9 is 3n. y(5)=2 is 3n+2.

ڭ̤]iHϥίx}󦡤Aɥݦbx}ҤsɡA󦡤~⦨ߡCyܻGuif x, expression; endvĩuif all(x(:)), expression; endvCOݪ`NOG x Oůx}ɡA󦡱NûߡC

MATLAB ]䴩 switch-case-otherwise hVOAϥλykpUG

switch expression case value(1) statement(1) case value(2) statement(2) ... case value(n-1) statement(n-1) otherwise statement(n) end

bWzykAexpression @ƭȩΦrAȩM value(k) ۵ɡAMATLAB Y statement(k) øX switch OCY expression value(k)Ak=1, 2, K, n-1Ah MATLAB | statement(n) øX switch OCҦpGھڤӧP_uOAiJpUG

Example 4: 16-{y{/switch01.mfor month = 1:12 switch month case {3,4,5} season = 'Spring'; case {6,7,8} season = 'Summer'; case {9,10,11} season = 'Autumn'; case {12,1,2} season = 'Winter'; end fprintf('Month %d ===> %s.\n', month, season); end Month 1 ===> Winter. Month 2 ===> Winter. Month 3 ===> Spring. Month 4 ===> Spring. Month 5 ===> Spring. Month 6 ===> Summer. Month 7 ===> Summer. Month 8 ===> Summer. Month 9 ===> Autumn. Month 10 ===> Autumn. Month 11 ===> Autumn. Month 12 ===> Winter.

bWҤAڭ̬ٲF otherwise ԭzCpG expression OrAYnb case hӦrANݨϥΦrꪺȰ}C]Cell Arrey of Strings^AҦpG

Example 5: 16-{y{/switch02.mmonth = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Dec', 'Nov', 'Dec'}; for i = 1:length(month) switch month{i} case {'Mar','Apr','May'} season = 'Spring'; case {'Jun','Jul','Aug'} season = 'Summer'; case {'Sep','Oct','Nov'} season = 'Autumn'; case {'Dec','Jan','Feb'} season = 'Winter'; end fprintf('%s is %s.\n', month{i}, season); endJan is Winter. Feb is Winter. Mar is Spring. Apr is Spring. May is Spring. Jun is Summer. Jul is Summer. Aug is Summer. Sep is Autumn. Dec is Winter. Nov is Autumn. Dec is Winter.

Hint
MATLAB switch OM C y switch OtOGb C y switch ԭzAC case ԭzݥ[W break HX switch ԭzAӦb MATLAB hh@|C

@ӨAswitchVcaseVotherwise IJvu ifVthenVelse C]AڭӦhhϥ switchVcaseVotherwiseC


MATLAB{]pGJg