CHAPTER 1: INTRODUCTION INTO MATLAB COMPUTING
Lecture Notes 1.2: Branch
statements and loops
Comparison operators: (1 if true, 0 if not)
·
equal: ==
e1 = (a == b) % it is false
e2 = (a == c) % it is true
e2 = 1
·
not equal: ~=
e4 = (a~=c) % it is false
e4 = 0
·
less than: <
·
equal or less than: <=
e5 = (y <= x)
e6 = (z < x)
z = 0.1259
e5 = 1
e6 = 0
·
greater than: >
·
equal or greater than: >=
e8 = (z > x)
e8 = 1
·
comparison operators can be applied to vectors, they return vectors of
ones and zeros
y = [ 2 1 4 3 6 5 ]
e = ( x < y)
1 2
3 4 5 6
y =
2 1
4 3 6 5
e =
1 0 1 0 1 0
Logical
operators and composite comparisons:
a |
b |
& |
| |
~a |
~b |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
·
logical AND: &
e1 = ( (n >= 10) & (n <= 50) ) % it is equivalent to: 10
<= n <= 50
e2 = ( (m >= 10) & (m <= 50) )
e3 = ( (l >= 10) & (l <= 50) )
e2 = 0
e3 = 0
·
logical OR: |
e4 = ( (n
< 10) | (n > 50) ) % it is equivalent to: n < 10 or n > 50
e5 = ( (m < 10) | (m > 50) )
e6 = ( (l < 10) | (l > 50) )
e5 = 1
e6 = 1
·
logical NOT: ~ (the use of this operator can be avoided)
e7 = ( ~(n <= 50) ) % it is equivalent to: n > 50
e8 = ( ~(m == 10) ) % it is equivalent to: m ~= 10
e9 = ( ~(l > 10) ) % it is equivalent to: l <= 10
e8 = 1
e9 = 0
· Typical errors in using logical operators
( ( n > 10 ) | ( n < 15 ) ) % it is always true: n >
10 or n < 15
( ( m > 10 ) & ( m < 5 ) ) % it is always false: m < 5 and m > 10
ans = 0
·
any: returns
1 if any of the elements of a vector are non-zero, returns 0 otherwise
·
all: returns
1 if all of the elements of a vector are non-zero, returns 0 otherwise
·
find: returns
the indices of the elements of a vector that are non-zero.
x = round(sin(0:pi/5:2*pi)), r1 = any(x), r2 = all(x), r3 = find(x)
r1 = 1
r2 = 0
r3 = 2 3 4 5 7 8 9 10
Branch
statements:
·
if, elseif, else, end: (the parts with elseif and else are optional)
if <comparison>
<statements>
elseif <comparison>
<statements>
elseif <comparison>
<statements>
else
<statements>
end
if ( a == b ) % NB: useful identation of the MATLAB codes
c = a*b
elseif ( b ~= 0 )
c = a/b % this branch is
executed for the example
else
c = 0
end % c is assigned dynamically depending on a and b
·
for, end
for
<counts>
<statements>
end
y = x^2-5*x-3
end
y = -8.2500
y = -9
y = -9.2500
y = -9
for k = 1 : length(x)
y(k) = x(k)^2-5*x(k)-3 %
NB: dynamical creation of the vector y
end
y = -7.0000 -8.2500
y = -7.0000 -8.2500
-9.0000
y = -7.0000 -8.2500
-9.0000 -9.2500
y = -7.0000 -8.2500 -9.0000 -9.2500 -9.0000
·
while, end
while
< condition >
<statements>
end
while (n <= length(x))
y(n) = x(n)^2-5*x(n)-3 %
NB: MATLAB vectors are created as row-vectors
n = n + 1;
end
y = -7.0000 -8.2500
y = -7.0000 -8.2500
-9.0000
y = -7.0000 -8.2500
-9.0000 -9.2500
y = -7.0000 -8.2500 -9.0000 -9.2500 -9.0000
·
break: terminate execution of a (for/end or while/end) loop
while (1) % infinite loop
r = 2*r;
if ( r > 100 )
break; % termination of
the infinite loop
end
V = 4*pi*r^3/3
end
V = 2.6808e+005
V = 2.1447e+006
When
break is used in nested loops, only the immediate loop, where break is
located, is terminated.