Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | emory-park |
View: | 229 times |
Download: | 2 times |
ARRAYS
The important use of arrays as indices has to do with picking out the elements of a vector (or array) that have a certain property or characteristic. To do this requires to things: definition of the characteristic "Finding" the indices of the vector with that
property
Boolean test on Vectors
Consider the vector A = [ 5 3 1 7 -3 -1 9 27 6 1]
Suppose want non-negative elements. Use a logic comparison to locate: Need notion of true/false – called
Boolean Only two possible values (true or false) In Matlab false is 0, true is 1
Relational Operators
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Comparisons
Comparisons are either true or false Most computer programs use the number 1
for true and 0 for false
The results of a comparison are used in selection structures and repetition structures to make choices
Comparisons
In order for MATLAB to decide a comparison is true for an entire matrix, it must be true for every element in the matrix
Simple “if” statement
if comparison statementsend
For example….
if G<50 count = count +1; disp(G);end
IF statements
Each branch can include multiple statements Statements can also include other if statements (can
nest if statements inside if statements)
if A>10
% computations;
end
ifcondition
statements
true
false
if A>10 % computations;else % computations;end
ifcondition
statements (1)
true false
statements (2)
The if/else structure
The simple if triggers the execution of a block of code if a condition is true
If it is false that block of code is skipped, and the program continues without doing anything
What if instead you want to execute an alternate set of code if the condition is false?
Flow chart of an if/else structure
Block of code to execute if the
comparison is true
ComparisonTrue False
Block of code to execute if the
comparison is false
Calculation Example
Use an if structure to calculate a natural log Check to see if the input is positive
If it is, calculate the natural log If it isn’t, send an error message to the screen
The if/else/elseif structure Use the elseif for multiple selection criteria For example
if (grade >= 90) Letter = ‘A’Disp(‘You did great’) % note disp function
Elseif (grade >= 80) % didn’t need <90Letter = ‘B’
Elseif (grade >= 70)Letter = ‘C’
Elseif (grade >= 60)Letter = ‘D’
Else Letter = ‘F’
End
Examples
• Write a program to determine if an applicant is eligible to drive
Start
if age<16 TrueSorry – You’ll have to wait
age<18You may have a youth license
age<70
True
You may have a standard license
True
Drivers over 70 require a special license
elseif
elseif
else
switch/case
This structure is an alternative to the if/else/elseif structure
The code is generally easier to read This structure allows you to choose between multiple
outcomes, based on some criterion, which must be exactly true
The criterion can be either a scalar (a number) or a string.
In practice, it is used more with strings than with numbers.
The structure of switch/case
switch variablecase option1
code to be executed if variable is exactly equal to option 1
case option2code to be executed if variable is exactly equal to option 2…
case option_ncode to be executed if variable is exactly
equal to option notherwise
code to be executed if variable is not equal to any of the options
end
Menu
The menu function is often used in conjunction with a switch/case structure.
This function causes a menu box to appear on the screen with a series of buttons defined by the programmer.
Because the input is controlled by a menu box, the user can’t accidentally enter a bad choice
This means you don’t need the otherwise portion of the switch/case structure