MATLAB SELECTION STATEMENTS (IF
& SWITCH)
>> aBin =
'10011010', aDec=bin2dec(aBin)
aBin =
10011010
aDec =
154
>> bBin
='11110001', bDec=bin2dec(bBin)
bBin =
11110001
bDec =
241
>>
andResult=bitand(aDec,bDec), andBin=dec2bin(andResult)
andResult =
144
andBin =
10010000
>>
orResult=bitor(aDec,bDec), orBin=dec2bin(orResult)
orResult =
251
orBin =
11111011
>> shift2=bitshift(aDec,2),
shiftBin=dec2bin(shift2)
shift2 =
616
shiftBin =
1001101000
20.9 Review
• Boolean
expressions for selective application of formulae or execution of code
• Relational
operators in Matlab
• Logical arrays
• find() function
• NaNs & empty
arrays
• base conversion
21 MATLAB SELECTION STATEMENTS (IF
& SWITCH)
• Selection between
alternate paths in code execution is a vital component of modern programming
languages:
- allows the
conditional execution of code
• Most non-trivial
programs (e.g., 50-lines+) in most programming languages are guaranteed to
have one or more IF-statements.
|
- Matlab is one of
these exceptions due to boolean operations on arrays.
|
• Matlab provides
two versions of the selection statement:
-
IF/IF-ELSE/IF-ELSEIF
- SWITCH-CASE
• Today’s lecture
covers:
- reasons for the widespread usage
of Selection statements
- syntax of Selection statements
in Matlab.
|
References: For Engineers (Ch. 1, 7)
Mastering
(Ch. 13)
Student
(Ch.. 11)
21.1 Motivation
• Branching (selection
between alternates) is a vital tool in solving most problems by computer:
- sequential execution implies
that the same code is executed every time regardless of inputs or other
conditions
- selection allows the execution
of the appropriate code for the current situation (set of values)
• Examples include:
- menu choices
if choice is “New”
then
create a new document
else if choice is
“Save” then
save the current document
:
else if choice is
“Quit” then
finish program
- error catching
if can’t open file
then
print error message and quit.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com |
- special-case handling &
variable outputs
if cost<1.0
fprintf(‘Price less than one dollar’);
else
fprintf(‘Price less than %d dollars’,…
ceil(cost));
end;
21.2 Simple IF
• Simplest form of
IF statement:
- conditional
execution of a single set of statements
• Schematic form:
statement_before;
if test
statement(s)_1;
end;
statement_after;
• test is a boolean
expression (evaluates to true or false)
- c.f. previous
lecture on relational operators
21.3 Examples – Simple IF
price =
input(‘Please enter a price for the item ‘);
if price<0
disp(‘A negative price input. Resetting to
positive’);
price = -price;
end;
fileName =
input(‘What is the name of the file containing the data ‘,’s’);
descriptor =
fopen(fileName,’r’);
if descriptor==-1
error(‘Unable to open file for reading.
Quitting’);
end;
if count~=0
average = total/count;
end;
21.4 IF-ELSE
• Often desirable to
make a choice between two alternates:
- do one or the
other but not both
• Schematic:
statement_before;
if test
statement(s)_1;
else
statement(s)_2;
end;
• Note layout
convention & indentation
21.5 Example – IF-ELSE
val = input(‘Please
input a value ‘);
if val<0
result = -val;
else
result = val;
end;
printf(‘The absolute
value of %d is %d\n’,...
|
val,result);
digit =
input(‘Please enter a single digit ‘);
if digit<10 &
digit>=0
fprintf(‘%d^%d is
%d\n’,digit,digit,digit^digit);
else
fprintf(‘Sorry, that was not a single
positive digit’);
end;
if
teamAScore>teamBscore
disp(‘Team A was the winner’);
else
disp(‘Team A did not win’);
end;
fileName =
input(‘What is the name of the file containing the data ‘,’s’);
descriptor =
fopen(fileName,’r’);
if descriptor==-1
error(‘Unable to open file for reading.
Quitting’);
else
values = fscanf(descriptor,’%f’);
end;
21.6 IF-ELSEIF-…
• Often (and more
generally) it is desirable to select one option (code segment to execute)
from multiple possibilities
• Schematic
statement_before;
if test1
statement(s)_1;
elseif test2
statement(s)_2;
elseif…
:
elseif testn
statement(s)_n;
else
statement(s)_n+1;
end;
statement_after;
• Performs each of
the checks in turn:
- as soon as first evaluates to
true performs corresponding statements & finishes
- if no test evaluates to true
then performs statements under else (which must be final)
IF-ELSEIF… (Cont)
21.7 Example – IF-ELSEIF…
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% finalMark.m
% Calculate a student's final mark in
Computer
% Tools based on a final lab and exam mark,
% plus whether they completed all competency
% exercises.
|
% An illustration of the IF-ELSEIF construct.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com % Author: Spike
% Date: 11/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
lab = input('Please
enter the lab mark as a percent [1-100] ');
exam = input('Please
enter the exam mark as a percent [1-100] ');
competent =
input('Did the student complete all competency exercises [y/n]? ','s');
initialTotal =
(lab+exam)/2;
if
initialTotal>39 & ~ strncmp(competent,'yes',1)
finalTotal = 39;
elseif
initialTotal>39 & lab<40
finalTotal = 39;
elseif
initialTotal>39 & exam<40
finalTotal = 39;
else
finalTotal = initialTotal;
end;
fprintf('The final
mark for the subject is %5.1f%%\n',finalTotal);
if
finalTotal~=initialTotal
fprintf('\t Reset from %5.1f%% due to
failure of a component\n',initialTotal);
end;
21.8 Truth Tables & Compound IFs
• IFs may be nested
(placed one inside the other).
|
- also called
compound
• Often hard to
follow logic of the code
- indentation helps readability
- truth tables help
• For example:
if test1
statements_1;
if test2 & test3
statements_2;
else
statements_3;
end;
elseif test4
statements_4
else
statements_5;
end;
T1 T2 T3 T4 S1 S2 S3 S4 S5
T F F * Y Y
T F T * Y Y
T T F * Y Y
T T T * Y Y
F * * F Y
F * * T Y
Compound IFs (Cont)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% finalMark2.m
% Calculate a student's final mark in
Computer
% Tools based on a final lab and exam mark,
% plus whether they completed all competency
% exercises.
|
% A variant on the finalMark script which
shows
% an alternate approach to the IF-ELSEIF
% construct as nested (compound) IFs.
|
KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Selasa, 13 Januari 2015
MATLAB SELECTION STATEMENTS (IF & SWITCH). KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar