Download presentation
Presentation is loading. Please wait.
1
Introduction to MATLAB
Session 2 Flow control String evaluation Mihaela Duta MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
2
Today’s topics Flow control Logical operators Relational operators
Logical indexing Flow control statements String evaluation MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
3
Logical operators and, or, negation, xor (exclusive or) Two types
b a xor b 1 and, or, negation, xor (exclusive or) Two types Element-wise Bit-wise and or negation xor Element-wise & | ~ Bit-wise bitand bitor bitcmp bitxor MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
4
Relational operators Perform comparisons
Output variables of logical type: true = logical(1) – if the relation holds false = logical(0) – if the relation doesn’t hold They are: a < b a less than b a <= b a less than or equal to b a > b a greater than b a >= b a greater than or equal to b a == b a equal to b a ~= b a not equal to b In the previous session we have worked with arithmetical operators. quantitative operations on the operands MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
5
How to apply relational operators
Two matrices of equal dimensions: to each corresponding element A matrix and a single value: to each matrix element in turn compared with the value MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
6
Logical indexing positional indexing use logical arrays
>> b = a( logical([ ]) ); equivalent to >> b = a(2); example: output of relational operators >> cmp = a > 30; >> a(cmp) >> a = [ ]; >> a>2 ans = >> a(a>2) MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
7
Flow control a series of MATLAB commands are executed in the order they appear to alter the order and allow for branching we have to use flow control statements: if-else switch for while break & return a = 2; b=4; b=5; a = 5; disp(‘a is greater than 3’); disp(‘a is less than 3’) MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
8
if-else Syntax Steps: evaluate <logical-expression>
if <logical-expression> <statement series 1> else <statement series 2> end Steps: evaluate <logical-expression> if true, then execute <statement series 1> if false, then execute <statement series 2> MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
9
switch Syntax Steps: evaluate <variable/expression>
switch <variable/expression> case <value 1> <statement series 1> etc case <value n> <statement series n> otherwise <statement series n+1> end Steps: evaluate <variable/expression> check the result against several options <value i> - labels for the case branches execute the series of statements corresponding to one case label if none of the options is valid, then the set of statements corresponding to label otherwise is executed Equivalent to a series of if statements MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
10
for Syntax for <variable> = <start>:<step>:<stop> <series of statements> end Steps assign to <variable> the value <start> and execute the <series of statements> once add <step> to <variable> and execute <series of statement> again repeat the previous step till <variable> reaches the value <stop> MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
11
while Syntax while <expression> <series of statements> end Steps Evaluate <expression> and if true execute the <series of statements> once Repeat the previous step till <expression> becomes false If the statements inside the loop do not modify <expression>, we’ll have infinite looping because <expression> will never invalidate MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
12
break and return break is used inside a for or while loop to request the immediate and unconditional exit from the loop return causes the immediate exit from a function MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
13
String evaluation [out1, ...]= eval(<str_with_statement>)
where str is a string containing a statement the MATLAB interpreter executes the statement stored [out1, …] = feval(<string_fctname>, in1, …) the MATLAB interpreter executes function given by <string_fctname> with input arguments in1, … and output arguments out1, … MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.