2-4 程?流??制

MATLAB ѭưj]Loops^αP_]Conditions^{y{]Flow Control^OA²檺{ưOO for j]For-loop^A򥻨ϥλykG

For ܼ = Vq B⦡; end

䤤ܼƪȷ|Q̦]wVqC@ӤȡAӭư椶 for M end B⦡C]AYLN~pAB⦡檺Ʒ|VqסC|ҨӻAUCO|ͤ@Ӫ׬ 6 թMƦC]Harmonic Sequence^G

>> x = zeros(1,6); % wtmx} x @Ӻ 16 sx} >> for i = 1:6 >> x(i) = 1/i; >> end >> disp(x) % ܯx} x e 1.0000 0.5000 0.3333 0.2500 0.2000 0.1667

bWҤAx} x ̪O@ 16 jpsx}Ab for j餤Aܼ i Ȩ̦O 1 6]Yx} 1:6 [1 2 3 4 5 6 ] C@Ӥ^A]x} x i ӤȨ̦Q] 1/i C

t@ӱ`Ψ쪺{ưOO while j]While-loop^A򥻨ϥλykG

while B⦡; end

]NOAun󦡦ߡAB⦡N|@AQưCҦpe for j鲣ͪթMƦC@kAڭ̥i while jgpUG

>> x = zeros(1,6); % x O@ 16 sx} >> i = 1; >> while i <= 6 >> x(i) = 1/i; >> i = i + 1; >> end >> disp(x) % ܯx} x e 1.0000 0.5000 0.3333 0.2500 0.2000 0.1667

Hint
WXӨҤlAڭ̨ϥ zeros ӹwtm]Pre-allocate^F@ӾAjpx}CYwtmx}A{iA MATLAB ݭnʺAaW[]δp^x}jpA]ӭC{IJvCҥHbϥΤ@ӯx}ɡAYbƫeDjpAh̦nϥ zeros ones Oӹwtmx}һݪOjpAHWi{IJvC

MATLAB ]Ѩ̱P_ӱ{y{OA̱` if V else V end OզXA򥻨ϥΧΦG

if B⦡; else B⦡; end

dҦpUG

>> if rand(1,1) > 0.5 >> disp('Given random number is greater than 0.5.'); >> else >> disp('Given random number is smaller than 0.5.'); >> end Given random number is less than 0.5.

{y{AbѪĤQu{y{vN|ԺɪСC


MATLAB{]pGJg