Chapter 6 Programming in Matlab
Chapter 6 Programming in Matlab
Chapter 6 Programming in Matlab
Programming in MATLAB
Slide deck by
Dr. Greg Reese
Miami University
1
6.
0
6.
0
true
If this and that are true
If this or that is true, etc.
Relational operator:
into"
Example
>> x=8:12
x =
>> x>10
ans =
>> x==11
ans =
>> x>=7
ans =
10
11
12
1
8
1. Is a vector
2. Has a 0 or 1 corresponding to each
original element
>> x=8:12
x =
8
9
>> x>10
ans =
0
0
10
11
12
TIP
If results of relational
comparison stored in a vector,
can easily find the number of
elements that satisfy that
comparison, i.e., that are true,
by using sum command, which
returns sum of vector elements
Works because elements that are
TIP
EXAMPLE
How many of the numbers from
1-20 are prime?
Use MATLAB isprime command,
11
TIP
13
14
EXAMPLE
What are the numbers from 1-10
that are multiples of 3?
>> 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
>> multiplesOf3 = numbers(multiples)
multiplesOf3 =
3 6 9
15
Example
Think of numbers(multiples) as
pulling out of numbers all elements
that have a 1 in the corresponding
element of multiples
numbers
= 1
9 10
multiples = 0
numbers(multiples) =
9
16
EXAMPLE
What are the prime numbers from
1-20?
>> numbers = 1:20;
>> numbers( isprime(numbers) )
ans =
2 3 5 7 11 13 17 19
17
Logical operators:
18
19
21
and b
a|b does the logical OR operation on a
or b
~a does the logical NOT operation on a
Arguments to all logical operators are
numbers
Zero is "false"
Any non-zero number is "true"
22
evaluation of operator
Result is an array that has same
dimensions as other two but only
contains 1's and 0's
23
25
EXAMPLE
Child 12 or less years
Teenager more than 12 and less than
20 years
Adult 20 or more years
>> age=[45 47 15 13 11]
age = 45
47
15
13
11
26
EXAMPLE
Who is a teenager?
>> age=[45 47 15 13 11];
>> age>=13
ans =
1
1
1
>> age<=19
ans =
0
0
1
>> age>=13 & age<=19
ans =
0
0
1
27
EXAMPLE
>> age=[45 47 15 13 11]
age = 45
47
15
13
11
28
MATLAB also
has other
Boolean
functions
30
6.2 CONDITIONAL
STATEMENTS
A conditional statement is a
command that allows MATLAB
to decide whether or not to
execute some code that follows
the statement
Conditional statements almost
31
if conditional-expression
code line 1
Can be any number of lines of
code
code line 2
end
remaining lines of code
If the conditional expression is false,
MATLAB skips the lines of code that are
between the line with if and the line
with end. Then it continues with the
code after the end-line
33
if conditional-expression
code line 1
Can be any number of lines of
code
code line 2
end
remaining lines of code
The conditional expression is true if it
evaluates to a logical 1 or to a non-zero
number. The conditional expression is
false if it evaluates to a logical 0 or to a
numerical zero.
34
preceding code
if conditional-expression
code line 1
code line 2
end
following code
35
A flowchart is a diagram
that shows the code flow.
Some common flowchart
symbols are
represents a sequence of
commands
represents an if-statement
shows the direction of
code execution
36
37
40
EXAMPLE
if the caller is your best friend
talk for a long time
elseif the caller is a potential date
talk for a little bit and then set a time to meet
elseif the caller is your study-mate
talk until you get the answer to the hard
problem
elseif the caller is your mom
say you're busy and can't talk
else
have your room-mate say you'll call back later
41
42
43
if-elseif-else-end structure
gets hard to read if more than a
few elseif statements. A
clearer alternative is the
switch-case structure
switch-case slightly different
because choose code to
execute based on value of
scalar or string, not just
true/false
44
Concept is
switch name
case 'Bobby'
talk for a long time
case 'Susan'
talk for a little bit and then set a time to
meet
case 'Hubert'
talk until you get the answer to the hard
problem
case 'Mom'
say you're busy and can't talk
otherwise
have your room-mate say you'll call back
later
45
46
switch evaluates
switch-expression
If value is equal to
value1, executes
all commands up to
next case,
otherwise,
or end statement, i.e., Group 1
commands, then executes code after
end statement
If value is equal to value2, same as
above but Group 2 commands only
Etc.
47
If switch-expression not
48
TIP
6.4
LOOPS
50
6.4.1 for-end
Loops
51
6.4.1 for-end
Loops
Body of
loop
6.4.1 for-end
Loops
1. Loop sets k to f,
Body of
loop
and executes
commands between
for and the end commands,
i.e., executes body of loop
2. Loop sets k to f+s, executes body
3. Process repeats itself until k > t
4. Program then continues with commands
that follow end command
increment of 1
53
6.4.1 for-end
Loops
Body of
loop
54
6.4.1 for-end
Loops
Body of
loop
55
6.4.1 for-end
Loops
Body of
loop
change value of k
Each for command in a program
must have an end command
56
6.4.1 for-end
Loops
Body of
loop
displayed automatically
Can display value in each pass
6.4.1 for-end
Loops
EXAMPLE
Script
Output
k =
1
x =
1
k =
4
x = 16
k =
7
x = 49
k = 10
x = 100
After loop k = 10
for k=1:3:10
k
x = k^2
end
fprintf('After loop k = %d\n', k);
58
6.4.1 for-end
Loops
TIP
59
6.4.2 while-end
Loops
iterations
You do have a condition that you
can test and stop looping when it
is false. For example,
Keep reading data from a file until
6.4.2 while-end
Loops
1. Loop
evaluates
conditional-expression
2. If conditional-expression is
true, executes code in body,
then goes back to Step 1
3. If conditional-expression is
false, skips code in body and
goes to code after endstatement
61
6.4.2 while-end
Loops
variable
There must be some value of the
variable that makes the conditional
expression be false
62
6.4.2 while-end
Loops
EXAMPLE
This script
Makes this
output
x = 1
while x <= 15
x = 2*x
end
x =
1
x =
2
x =
4
x =
8
x =
16
63
6.4.2 while-end
Loops
6.4.2 while-end
Loops
65
6.4.2 while-end
Loops
never changes
minDistance = 42;
distanceIncrement = 0;
Typo should be
10
distance = 0;
while distance < minDistance
distance=distance+distanceIncrement;
end
66
6.4.2 while-end
Loops
expression changed
minDistance = 42;
delta = 10;
distance = 0;
while distance < minDistance
minDistance = minDistance + delta;
Typo should be
end
distance
67
6.4.2 while-end
Loops
becomes
false
Typo shouldn't be
any negative
sign
minDistance
= 42;
x = 0;
y = 0;
while -sqrt( x^2+y^2 ) < minDistance
x = x + 1;
y = y + x;
end
68
6.4.2 while-end
Loops
TIP
69
If a loop or conditional
statement is placed inside
another loop or conditional
statement, the former are said
to be nested in the latter.
Most common to hear of a nested
dimensional problems
70
EXAMPLE
71
72
73
EXAMPLE
Script
name
name
name
name
name
or
is
or
is
or
q to quit: Greg
Greg
q to quit: quentin
quentin
q to quit: q
74
EXAMPLE
for ii=1:100
if rem( ii, 8 ) == 0
count = 0;
fprintf('ii=%d\n',ii);
continue;
end
% code
% more code
end
76