Presentation is loading. Please wait. # Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:

## Presentation on theme: "Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:"— Presentation transcript:

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to: additional operators branches to control operations loops to repeat operations

Relational Operators in M ATLAB A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays (match the sizes on arrays!) – Numeric or string Operators: >> == = << =~ = Result is true (1) or false (0) – perhaps an array

Examples: expressionresult 5 < 71 [ 3 5 2 ] > = [ 1 0 12 ]1 1 0 max( 1:6 ) < = 7 1 [3 pi -12 ] > 11 1 0 'Tom' = = 'Bob'0 1 0 'Tom' = = 'm'0 0 1 Note – arrays and strings need to be the same size

Matlab has Logical Operators as Well A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays, numeric or string A and B are interpreted as logical (binary): – Numeric 0 is interpreted as false – All else is interpreted as true (equal to 1) Result is true (1) or false (0) – perhaps an array

Basic operators: and & or | xor not ~ 0 0 1 1 ~A~A 0 1 1 0 xor(A,B) 1001 1010 1111 0000 A|BA|BA&BBA “truth table”“unary” operator

Operator Precedence (left to right) 1. Parentheses ( ) 2. Transpose(') and power(.^) 3. Negation (-) and logical negation (~) 4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-) 6. Colon operator (:) 7. Relational operators (, >=, = =, ~=) 8. Logical AND (&) 9. Logical OR (|)

Branches, Conditional Statements Commands to select and execute certain blocks of code, skipping other blocks. Three types in Matlab: – if/else – switch – try/catch this week

“If/Else” Use relational and logical operators to determine what commands to execute: if expression {commands if true } else {commands if false } end evaluate this use of blue in editor; also, auto indentation on commands

Example – output whether a variable x is positive or not: x = … { computed somehow }; if x > 0 disp('the value is positive') else disp('the value is negative or zero') end

Example – output a warning if the variable x is negative (note that there is no “else” portion in this example): x = … { computed somehow }; if x < 0 disp( ' Warning: negative value ' ) end the else component is not required

Example – ask if a plot should be drawn: x = input( ' Plot now? ', ' s ' ); if x = = ' yes ' | x = = ' YES ' plot( ….. ) end more complicated expression to evaluate

Example – Write a script to put 2 numbers in numerical order:

Loops Commands to repeatedly execute certain blocks of code Two types in Matlab: – for – while this week

The “for” Loop Used for a specific number of repetitions of a group of commands: for index = array { commands to be repeated go here } end Rules: One repetition per column of array index takes on the corresponding column’s values

Example – implement a count down timer (in seconds):

Similar presentations

Ads by Google