Upload
ken-loo
View
246
Download
0
Embed Size (px)
Citation preview
7/23/2019 Chapter 6 Programming in Matlab
1/76
1
Slide deck by
Dr. Greg ReeseMiami University
Chapter 6Programming in MATLAB
7/23/2019 Chapter 6 Programming in Matlab
2/76
!."
#n t$is c$a%ter &ill st'dy $o&to make MATLAB %rogramsr'n sections o( code
#( somet$ing is tr'e
)$ile somet$ing is tr'e
*or a certain n'mber o(times
7/23/2019 Chapter 6 Programming in Matlab
3/76
+
!."
)ill also learn $o& to r'n
di,erent sections o( codede%ending onT$e val'e o( a variable
)$ic$ %artic'lar condition is tr'e )$at combination o( conditions istr'e
#( t$is and t$at are tr'e#( t$is or t$at is tr'e- etc.
)$at relations$i% t&o t$ings $ave
*or eam%le- one is less t$an t$e
7/23/2019 Chapter 6 Programming in Matlab
4/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
Relational operator:
5an6t %'t s%ace bet&een o%erators t$at$ave t&o c$aracters
74ot e0'al to7 is 7897- not 7:97 as in 5or 5;;
720'al to7 com%arison is t&o e0'alsigns
7/23/2019 Chapter 6 Programming in Matlab
5/76
>
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
Res'lt o( com%aring &it$ arelational o%erator is al&ays 7tr'e7
or 7(alse7#( 7tr'e7- MATLAB gives t$e
com%arison a val'e o( one
7/23/2019 Chapter 6 Programming in Matlab
6/76
!
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
)$en com%aring arraysT$ey m'st be t$e samedimensions
MATLAB does an element@&isecom%arison
Res'lt is an array t$at $as
same dimensions as ot$er t&ob't only contains 16s and "6s
7/23/2019 Chapter 6 Programming in Matlab
7/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
)$en com%aring array toscalar
MATLAB com%ares scalar toevery member o( array
Res'lt is an array t$at $as
same dimensions as originalb't only contains 16s and "6s
7/23/2019 Chapter 6 Programming in Matlab
8/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2am%le
>> x=8:12
x = 8 9 10 11 12
>> x>10
ans = 0 0 0 1 1
>> x==11
ans = 0 0 0 1 0
>> x>=7
ans = 1 1 1 1 1
7/23/2019 Chapter 6 Programming in Matlab
9/76C
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
#t $el%s to %ict're in yo'r mind t$at
t$e res'lt o( a logical com%arison1. #s a vector
. as a " or 1 corres%onding to eac$original element
>> x=8:12
x = 8 9 10 11 12
>> x>10
ans = 0 0 0 1 1
T # P
7/23/2019 Chapter 6 Programming in Matlab
10/761"
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
#( res'lts o( relationalcom%arison stored in a vector-can easily End t$e n'mber o(
elements t$at satis(y t$atcom%arison- i.e.- t$at are tr'e-by 'sing sumcommand- &$ic$
ret'rns s'm o( vector elements )orks beca'se elements t$at aretr'e $ave val'e o( one and (alse
elements $ave val'e ?ero
T # P
7/23/2019 Chapter 6 Programming in Matlab
11/7611
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
o& many o( t$e n'mbers (rom1@" are %rime Use MATLAB isprimecommand-
&$ic$ ret'rns tr'e
7/23/2019 Chapter 6 Programming in Matlab
12/761
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
5an mi relational and
arit$metic o%erations in onee%ression Arit$metic o%erations (ollo& 's'al
%recedence and al&ays $ave$ig$er %recedence t$an relationalo%erations
Relational o%erations all $avee0'al %recedence and eval'atedle(t to rig$t
T # P
7/23/2019 Chapter 6 Programming in Matlab
13/761+
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
A logical vectoror logical array
is a vectorHarray t$at $as onlylogical16s and "6s
16s and "6s (rom mat$ematicalo%erations don6t co'nt
16s and "6s (rom relational
com%arisons do &ork *irst time a logical vectorHarray'sed in arit$metic- MATLAB
c$anges it to a n'merical
7/23/2019 Chapter 6 Programming in Matlab
14/761
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
5an 'se logical vector to get
act'al val'es t$at satis(y relation-not I'st &$et$er or not relationsatisEed. Doing t$is is called
logical indexingor logicalsubscripting Do t$is by 'sing logical vector as
inde in vector o( val'es. Res'lt isval'es t$at satis(y relation- i.e.-val'es (or &$ic$ relations$i% are 1
43T2 J tec$ni0'e doesn6t 0'ite &ork &it$
arrays. )on6t disc'ss t$at case ('rt$er
7/23/2019 Chapter 6 Programming in Matlab
15/761>
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
)$at are t$e n'mbers (rom 1@1"t$at are m'lti%les o( +>> numbers = 1:10
numbers = 1 2 3 4 5 6 7 8 9 10>> multiples = rem( numbers, 3 ) == 0
multiples = 0 0 1 0 0 1 0 0 1 0
>> multiples!3 = numbers(multiples)multiples!3 =
3 6 9
7/23/2019 Chapter 6 Programming in Matlab
16/761!
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2am%le
T$ink o( numbers(multiples)as%'lling o't o( numbersall elementst$at $ave a 1in t$e corres%onding
element o( multiplesnumbers = 1 2 3 4 5 6 7 8 9 10
multiples = 0 0 1 0 0 1 0 0 1 0
numbers(multiples) = 3 6 9
7/23/2019 Chapter 6 Programming in Matlab
17/761
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
)$at are t$e %rime n'mbers (rom1@">> numbers = 1:20;
>> numbers( isprime(numbers) )
ans =
2 3 5 7 11 13 17 19
Logical indeing is %artic'larly'se('l &$en 'sed &it$ logical
o%erators- disc'ssed net
7/23/2019 Chapter 6 Programming in Matlab
18/761
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
Logical operators:
Boolean logic is a system (orcombining e%ressions t$at areeit$er tr'e o( (alse.
MATLAB $as o%erators andcommands to do many Booleano%erations
Boolean o%erations incombination &it$ relationalcommands let yo' %er(ormcertain t es o( com 'tations
7/23/2019 Chapter 6 Programming in Matlab
19/76
1C
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
A truth table deEnes t$e la&s o(
Boolean logic. #t gives t$e o't%'t o(a logical o%eration (or every%ossible combination o( in%'ts. T$e
tr't$ table relevant to MATLAB is
7/23/2019 Chapter 6 Programming in Matlab
20/76
"
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
#n &ords- t$e tr't$ table says A4D is tr'e i( bot$ in%'ts aretr'e- ot$er&ise it is (alse
3R is tr'e i( at least one in%'t is
tr'e- ot$er&ise it is (alse
F3R
7/23/2019 Chapter 6 Programming in Matlab
21/76
1
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
An arit$metic o%erator- e.g.- ; or
@- is a symbol t$at ca'ses MATLABto %er(orm an arit$meticalo%eration 'sing t$e n'mbers or
e%ressions on eit$er side o( t$esymbol.
Similarly- a logical operatoris ac$aracter t$at makes MATLAB%er(orm a logical o%eration on one
or t&o n'mbers or e%ressions.
7/23/2019 Chapter 6 Programming in Matlab
22/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
MATLAB $as t$ree logical o%erators
"- #- $ a"bdoes t$e logical A4D o%eration on aand b
a#bdoes t$e logical 3R o%eration on aor b
$a does t$e logical 43T o%eration on a
Arg'ments to all logical o%erators aren'mbersero is 7(alse7
Any non@?ero n'mber is 7tr'e7
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
23/76
+
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
)$en 'sing logical o%erator onarraysT$ey m'st be t$e same dimensions
MATLAB does an element@&iseeval'ation o( o%erator
Res'lt is an array t$at $as samedimensions as ot$er t&o b't onlycontains 16s and "6s
7/23/2019 Chapter 6 Programming in Matlab
24/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
)$en o%erating &it$ array andscalar
MATLAB does element@&iseo%eration on eac$ array element&it$ scalar
Res'lt is an array t$at $as samedimensions as original b't onlycontains 16s and "6s
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
25/76
>
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
5an combine arit$metic-
relational o%erators- and logicalo%erators. 3rder o( %recedenceis
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
26/76
!
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
5$ild J 1 or less yearsTeenager J more t$an 1 and less t$an
" years
Ad'lt J " or more years>> a&e='45 47 15 13 11
a&e = 45 47 15 13 11
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
27/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
)$o is a teenager>> a&e='45 47 15 13 11;
>> a&e>=13
ans = 1 1 1 1 0
>> a&e=19
ans = 0 0 1 1 1
>> a&e>=13 " a&e=19
ans = 0 0 1 1 0
T$ese mark t$e t&o
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
28/76
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
2FAMPL2
>> a&e='45 47 15 13 11a&e = 45 47 15 13 11
)$o is not a teenager
>> $(a&e>=13 " a&e=19)
ans = 1 1 0 0 1
)$o is an ad'lt or a c$ild
>> a&e>19 # a&e13
ans = 1 1 0 0 1
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
29/76
C
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
Built-in logical functions:
MATLAB $as some b'ilt@in('nctions or commands (or doinglogical o%erations and related
calc'lations. T$ree are e0'ivalentto t$e logical o%erators
an*(+,)J same as +"
%r(+,)J same as +#
n%t(+)J same as $+
! 1 R2LAT#34AL A4D L3G#5AL
7/23/2019 Chapter 6 Programming in Matlab
30/76
+"
!.1 R2LAT#34AL A4D L3G#5AL3P2RAT3RS
MATLAB also$as ot$er
Boolean('nctions
! 534D#T#34AL
7/23/2019 Chapter 6 Programming in Matlab
31/76
+1
!. 534D#T#34ALSTAT2M24TS
A conditional statementis a
command t$at allo&s MATLABto decide &$et$er or not toeec'te some code t$at (ollo&s
t$e statement 5onditional statements almostal&ays %art o( scri%ts or ('nctions
T$ey $ave t$ree general (ormsi!-en*
i!-else-en*
i!-elsei!-else-en*
! 1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
32/76
+
!..1 T$ei!-en*Str'ct're
An i!-en*statement $as t$e (orm
i! conditional@e%ression
code line 1
code line
en*
remaining lines o( code
#( t$e conditional e%ression is tr'e-MATLAB r'ns t$e lines o( code t$at arebet&een t$e line &it$ i! and t$e line&it$ en*. T$en it contin'es &it$ t$e codea(ter t$e en*@line
5an be any n'mber o( lines o(code
! 1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
33/76
++
!..1 T$ei! en*Str'ct're
i! conditional@e%ression
code line 1code line
en*
remaining lines o( code
#( t$e conditional e%ression is (alse-
MATLAB ski%s t$e lines o( code t$at arebet&een t$e line &it$ i! and t$e line&it$ en*. T$en it contin'es &it$ t$e
code a(ter t$e en*@line
5an be any n'mber o( lines o(code
! 1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
34/76
+
!..1 T$ei! en*Str'ct're
i! conditional@e%ression
code line 1code line
en*
remaining lines o( code
T$e conditional e%ression is tr'e i( it
eval'ates to a logical 1 or to a non@?eron'mber. T$e conditional e%ression is(alse i( it eval'ates to a logical " or to a
n'merical ?ero.
5an be any n'mber o( lines o(code
! 1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
35/76
+>
!..1 T$ei! en*Str'ct're
%receding code
i! conditional@e%ression
code line 1code line
en*
(ollo&ing code
T$e above s$o&s t$e i!-en*
statement in a &ay t$at em%$asi?es&$at t$e contents o( t$e lines o(code. To em%$asi?e t$e code fow-i.e.- t$e order in &$ic$ t$e code iseec'ted- can 'se a No&c$art
!..1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
36/76
+!
!..1 T$ei! en*Str'ct're
A fowchartis a diagramt$at s$o&s t$e code No&.Some common No&c$artsymbols are
re%resents a se0'ence o(commands
re%resents an i(@statement s$o&s t$e direction o(code eec'tion
!..1 T$e i!-en*
7/23/2019 Chapter 6 Programming in Matlab
37/76
+
!..1 T$ei! en*Str'ct're
*ig. !@1 s$o&s t$e i(@end str'ct're asbot$ lines o( code and a No&c$art.
4ote $o& t$e diamond symbolclearly s$o&s t$at t$e code No& isdi,erent i( t$e conditional
e%ression is tr'e t$an i( it is (alse
!.. T$ei!-else-en*
7/23/2019 Chapter 6 Programming in Matlab
38/76
+
Str'ct're
i!-else-en*str'ct're lets yo'
eec'te one section o( code i( acondition is tr'e and a di,erentsection o( code i( it is (alse.
2FAMPL2 @ ans&ering yo'r %$onei! t$e caller is yo'r best (riend
talk (or a long time
elsetalk (or a s$ort time
en*
!.. T$ei!-else-en*
7/23/2019 Chapter 6 Programming in Matlab
39/76
+C
Str'ct're
*ig. !@ s$o&s t$e code andt$e No&c$art (or t$e i!-else-en*str'ct're
!..+ T$ei!-elsei!-else-en*
7/23/2019 Chapter 6 Programming in Matlab
40/76
"
Str'ct're
i!-elsei!-else-en*str'ct're lets
yo' c$oose one o( t$ree
7/23/2019 Chapter 6 Programming in Matlab
41/76
1
Str'ct're
5an $ave as many elsei!statements asyo' &ant
2FAMPL2i! t$e caller is yo'r best (riend
talk (or a long time
elsei!t$e caller is a %otential date
talk (or a little bit and t$en set a time to meet
elsei!t$e caller is yo'r st'dy@mate
talk 'ntil yo' get t$e ans&er to t$e $ard
%roblemelsei!t$e caller is yo'r mom
say yo'6re b'sy and can6t talk
else
$ave yo'r room@mate say yo'6ll call back later
!..+ T$ei!-elsei!-else-en*
7/23/2019 Chapter 6 Programming in Matlab
42/76
Str'ct're
*ig. !@+ s$o&s t$e code and t$eNo&c$art (or t$e i!-elsei!-else-en*str'ct're
!..+ T$ei!-elsei!-else-en*
7/23/2019 Chapter 6 Programming in Matlab
43/76
+
Str'ct're
5an omit elsestatement
#n t$is case- i( no matc$ toi!@ or elsei!@statements-no code in str'ct're gets
eec'ted
!.+ T2 s.it/-/ase
7/23/2019 Chapter 6 Programming in Matlab
44/76
STAT2M24T
i!-elsei!-else-en*str'ct're
gets $ard to read i( more t$an a(e& elsei!statements. Aclearer alternative is t$e
s.it/-/asestr'ct'res.it/-/aseslig$tly di,erentbeca'se c$oose code to
eec'te based on val'e o(scalar or string- not I'sttr'eH(alse
!.+ T2 s.it/-/ase
7/23/2019 Chapter 6 Programming in Matlab
45/76
>
STAT2M24T
5once%t iss.it/ name
/ase %bb
talk (or a long time
/ase usan
talk (or a little bit and t$en set a time to
meet
/ase ubert
talk 'ntil yo' get t$e ans&er to t$e $ard%roblem
/ase %m
say yo'6re b'sy and can6t talk
%ter.ise
$ave yo'r room@mate say yo'6ll call backlater
!.+ T2 s.it/-/ase
7/23/2019 Chapter 6 Programming in Matlab
46/76
!
STAT2M24T
!.+ T2 s.it/-/aseSTAT2M24T
7/23/2019 Chapter 6 Programming in Matlab
47/76
STAT2M24T
s.it/eval'atess&itc$@e%ression#( val'e is e0'al toalue1- eec'tesall commands '% to
net /ase-%ter.ise,
or en*statement- i.e.- Gro'% 1commands- t$en eec'tes code a(ter
en*statement#( val'e is e0'al to alue2- same as
above b't Gro'% commands only
2tc.
!.+ T2 s.it/-/aseSTAT2M24T
7/23/2019 Chapter 6 Programming in Matlab
48/76
STAT2M24T#( s&itc$@e%ression not
e0'al to any o( val'es in/asestatement-commands a(ter%ter.iseeec'ted. #(%ter.isenot %resent-
no commands eec'ted #( s&itc$ e%ression matc$es more t$anone /aseval'e- only Erst matc$ing/aseeec'ted
!.+ T2 s.it/-/aseSTAT2M24T
7/23/2019 Chapter 6 Programming in Matlab
49/76
C
STAT2M24T
5om%arisons o( tet strings are case@sensitive. #( /aseval'es are tet
strings- make all val'es eit$er lo&ercase or '%%er case- t$en 'se upperorl%.ercommand to convert s&itc$e%ression
/aller = l%.er( name );
s.it/ /aller
/ase b%bb
some code
/ase susan
some code
/ase m%m
some code
T # P
!.L33PS
7/23/2019 Chapter 6 Programming in Matlab
50/76
>"
L33PS
A loopis anot$er met$od o(
No& control. A loo% eec'tesone set o( commandsre%eatedly. MATLAB $as t&o
&ays to control n'mber o(times loo% eec'tes commands Met$od 1 J loo% eec'tescommands a s%eciEed n'mber o(times
Met$od J loo% eec'tes
commands as long as a s%eciEed
!..1 !%r-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
51/76
>1
Loo%s
A !%r-en*loo%
7/23/2019 Chapter 6 Programming in Matlab
52/76
>
Loo%s
T$e loo% inde variable can $ave anyvariable name
7/23/2019 Chapter 6 Programming in Matlab
53/76
>+
Loo%s
1. Loo% sets to !-and eec'tescommands bet&een!%rand t$e en*commands-i.e.- eec'tes body o( loo%
. Loo% sets to !s- eec'tes body+. Process re%eats itsel( 'ntil > t
. Program t$en contin'es &it$ commandst$at (ollo& en*command
.!and tare 's'ally integers
.s's'ally omitted. #( so- loo% 'sesincrement o( 1
Body o(loo%
!..1 !%r-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
54/76
>
Loo%s
#ncrement scan be negative*or eam%le- = 25:5:10%rod'ces
(o'r %asses &it$ = 25, 20, 15, 10
#( ! = t- loo% eec'tes once
#( ! > tand s > 0- or i( ! t ands 0,loo% not eec'ted
Body o(loo%
!..1 !%r-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
55/76
>>
Loo%s
#( val'es o( - s- and tare s'c$ t$at
cannot be e0'al to t- t$en#( s%ositive- last %ass is one &$ere $as
largest val'e smaller t$an t
*or eam%le- = 8:10:50%rod'ces Eve%asses &it$ = 8, 18, 28, 38, 48
#( s is negative- last %ass is one &$ere $as smallest val'e larger t$an t
Body o(loo%
!..1 !%r-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
56/76
>!
Loo%s
#n t$e !%rcommand can also be
assigned s%eciEc val'e
7/23/2019 Chapter 6 Programming in Matlab
57/76
>
Loo%s
Oal'e o( loo% inde variable
7/23/2019 Chapter 6 Programming in Matlab
58/76
>
Loo%s
2FAMPL2
Scri%t!%r =1:3:10
x = 2
en*
!print!(+!ter l%%p = *
7/23/2019 Chapter 6 Programming in Matlab
59/76
>C
Loo%s
5an o(ten calc'latesomet$ing 'sing eit$er a (or@
loo% or element&iseo%erations.
2lement&ise o%erations are 3(ten (aster
3(ten easier to read
More MATLAB@likeG242RAL ADO#52 J 'seelement&ise o%erations &$en
yo' can- (or@loo%s &$en yo'
T # P
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
60/76
!"
Loo%s
.ile-en*loo% 'sed &$en
o' don6t kno& n'mber o( loo%iterations
o' do $ave a condition t$at yo'
can test and sto% loo%ing &$en itis (alse. *or eam%le-Qee% reading data (rom a Ele 'ntil
yo' reac$ t$e end o( t$e Ele
Qee% adding terms to a s'm 'ntil t$edi,erence o( t$e last t&o terms isless t$an a certain amo'nt
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
61/76
!1
Loo%s
1. Loo%
eval'atesconditional@e%ression
. #( conditional@e%ression is
tr'e- eec'tes code in body-t$en goes back to Ste% 1
+. #( conditional@e%ression is
(alse- ski%s code in body andgoes to code a(ter en*@statement
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
62/76
!
oo%s
T$e conditional e%ression o( a.ile-en*loo%
as a variable in itBody o( loo% m'st c$ange val'e o(variable
T$ere m'st be some val'e o( t$e
variable t$at makes t$e conditionale%ression be (alse
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
63/76
!+
%
2FAMPL2
T$is scri%tx = 1
.ile x = 15
x = 2xen*
Makes t$is
o't%'tx =
1
x =
2
x =
4
x =
8
x =
16
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
64/76
!
%
#( t$e conditional e%ression
never becomes (alse- t$e loo%&ill kee% eec'ting... (orever:
T$e book calls t$is an indenite
loop, b't more commonlyre(erred to as an innite loop.
o'r %rogram &ill I'st kee%
r'nning- and i( t$ere is no o't%'t(rom t$e loo%
7/23/2019 Chapter 6 Programming in Matlab
65/76
!>
%
5ommon ca'ses o( indeEnite loo%s 4o variable in conditional e%ression
*istan/e1 = 1;
*istan/e2 = 10;*istan/e3 = 0;
.ile *istan/e1 *istan/e2
!print!(istan/e = *
7/23/2019 Chapter 6 Programming in Matlab
66/76
!!
%
5ommon ca'ses o( indeEnite loo%s Oariable in conditional e%ressionnever c$anges
ministan/e = 42;
*istan/e?n/rement = 0;
*istan/e = 0;
.ile *istan/e ministan/e
*istan/e=*istan/e*istan/e?n/rement;en*
Ty%o J s$o'ld be1"
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
67/76
!
5ommon ca'ses o( indeEnite
loo%s )rong variable in conditionale%ression c$anged
ministan/e = 42;*elta = 10;
*istan/e = 0;
.ile *istan/e ministan/e
ministan/e = ministan/e *elta;
en* Ty%o J s$o'ld bedistance
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
68/76
!
5ommon ca'ses o( indeEniteloo%s 5onditional e%ression neverbecomes (alse
ministan/e = 42;
x = 0;
= 0;
.ile -s@rt( x22 ) ministan/e
x = x 1;
= x;
en*
Ty%o J s$o'ldn6t beany negative sign
!.. .ile-en*Loo%s
7/23/2019 Chapter 6 Programming in Matlab
69/76
!C
#( yo'r %rogram gets ca'g$tin an indeEnite loo%-
P't t$e c'rsor in t$e5ommand )indo&
Press 5TRL;5
T # P
!.> 42ST2D L33PS A4D 42ST2D 534D#T#34ALSTAT2M24TS
7/23/2019 Chapter 6 Programming in Matlab
70/76
"
#( a loo% or conditional
statement is %laced insideanot$er loo% or conditionalstatement- t$e (ormer are said
to be nestedin t$e latter. Most common to $ear o( a nestedloop- i.e.- a loo% &it$in a loo%
3(ten occ'r &$en &orking &it$ t&o@dimensional %roblems
2ac$ loo% and conditionalstatement m'st $ave an en*
!.> 42ST2D L33PS A4D 42ST2D 534D#T#34ALSTAT2M24TS
7/23/2019 Chapter 6 Programming in Matlab
71/76
1
2FAMPL2
!.! T2 breaA4D /%ntinue53MMA4DS
7/23/2019 Chapter 6 Programming in Matlab
72/76
The breacommand:
)$en inside a loo%
7/23/2019 Chapter 6 Programming in Matlab
73/76
+
breacommand in scri%t or ('nctionEle b't not in a loo% terminateseec'tion o( Ele
breacommand 's'ally 'sed &it$ina conditional statement.#n loo%s %rovides &ay to end loo%ing i(
some condition is met
!.! T2 breaA4D /%ntinue53MMA4DS
2FAMPL2
7/23/2019 Chapter 6 Programming in Matlab
74/76
2FAMPL2Scri%t
.ile( 1 ) name = input( Ape name %r @ t% @uit: , s );
i! len&t( name ) == 1 "" name(1) == @
brea;
else
!print!( B%ur name is s
7/23/2019 Chapter 6 Programming in Matlab
75/76
>
The /%ntinuecommand:
Use /%ntinueinside a loo%
7/23/2019 Chapter 6 Programming in Matlab
76/76
2FAMPL2!%r ii=1:100
i! rem( ii, 8 ) == 0
/%unt = 0;
!print!(ii=*