【正文】
? 一般而言, switch–case–otherwise 的執(zhí)行效率優(yōu)於 if–else 。, month{i}, season)。 end fprintf(39。} season = 39。,39。,39。 case {39。} season = 39。,39。,39。 case {39。} season = 39。,39。,39。 case {39。} season = 39。,39。,39。 for i = 1:length(month) switch month{i} case {39。Sep39。Aug39。Jul39。Jun39。May39。Apr39。Mar39。Feb39。Jan39。, month, season)。 end fprintf(39。 case {12,1,2} season = 39。 case {9,10,11} season = 39。 case {6,7,8} season = 39。 MATLAB 程式設(shè)計(jì)入門(mén)篇:程式流程控制 程式流程控制之範(fàn)例十一 ? 欲根據(jù)月份來(lái)判斷其季別,可輸入如下: ? 範(fàn)例 1611 : for month = 1:12 switch month case {3,4,5} season = 39。 end end y(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. MATLAB 程式設(shè)計(jì)入門(mén)篇:程式流程控制 switchcaseotherwise 條件指令 ? MATLAB 在第五版開(kāi)始支援 switchcaseotherwise 的多向條件指令,其使用語(yǔ)法如下: switch expression case value(1) statement(1) case value(2) statement(2) … case value(n1) statement(n1) otherwise statement(n) end ? 在上述語(yǔ)法中, expression 為一數(shù)值或字串,當(dāng)其值和 value(k) 相等時(shí),MATLAB 即執(zhí)行 statement(k) 並跳出 switch 指令。y(%g)=%g is 3n+2.\n39。, i , y(i))。 elseif rem(y(i), 3)==1 fprintf(39。y(%g)=%g is 3n.\n39。例如,欲判斷 y 向量之元素是屬於 3n、 3n+ 或 3n+2,可輸入如下: ? 範(fàn)例 1610 : y = [3 4 5 9 2]。 end end y(1) = 0 is even. y(2) = 3 is odd. y(3) = 4 is even. y(4) = 1