LogicalAndRelationalOperators
LogicalAndRelationalOperators
Relational Operators
Relational operators are used to
represent conditions
such as “space ≤ 0” and “result ≠ 25”
They take two numerical (or string)
operands
They yield a logical result (true or false)
5
Relational Operators
The general form is: a1 op a2
a1 and a2 are arithmetic expressions, variables, or
strings.
op is one of the following
Operator Operation
== Equal to
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
6
Relational Operators
Operation Result
3<4 true (1)
3 <= 4 true (1)
3 == 4 false (0)
3 ~= 4 true (1)
3>4 false (0)
4 >= 4 true (1)
Characters are evaluated
'A' < 'B’ true (1)
in alphabetical order
7
Relational Operators
Relational operators may be used
To compare a scalar value with an array
a = [1 10; -2 3];
b = 3;
c = (a >= b)
c=
0 1
0 1
To compare two arrays
x= [1 10; -2 3]; x = [1 10; -2 3];
y= [4 5; 6 7]; y = [4 5; 6 7; 2 0];
x< y x<y
ans = ??? Error using ==> <
1 0 Matrix dimensions must agree.
1 1
8
Relational Operators
Don’t confuse equivalence (==) with
assignment (=)
Relational operations have lower priority than
arithmetic operations
i.e., relational operators are evaluated after all
arithmetic operators have been evaluated
You can use parentheses to be safe
14 + 4 < 3 * 5 (14 + 4) < (3 * 5)
ans = ans =
0 0
9
Relational Operators
Be careful about roundoff errors during numeric
comparisons (== and ~= operators)
a = 0;
b = sin(pi);
a == b
ans =
0 (since sin(pi) calculation yields 1.2246e-016)
Logical Operators
More complex conditions can be
represented
by combining relational operations using
logical operators
“temperature ≠ 25” AND “humidity < 60 %”
“exam grade < 45” OR “attendance ≤ 75”
They take one or two logical operands
They yield a logical result (true or false)
11
Logical Operators
The general form of a binary logic operation: b1 op b2
The general form of a unary logic operation: op b1
b1 and b2 are expressions or variables
op is one of the following
Operator Operation
& Logical AND
&& Logical AND with shortcut evaluation
| Logical OR
|| Logical OR with shortcut evaluation
xor Logical exclusive OR
~ Logical NOT
12
Logical ANDs
b1 b2 b1 & b2 b1 && b2
0 0 0 0
0 1 0 0
1 0 0 0
1 1 1 1
Logical ANDs
Most of the time, it does not matter which AND operation is used.
b = 4; a = 8;
(b ~= 6) & (a > 4) (b ~= 6) && (a > 4)
ans = ans =
1 1
Logical ORs
b1 b2 b1 | b2 b1 || b2
0 0 0 0
0 1 1 1
1 0 1 1
1 1 1 1
Hierarchy of Operators
1) Parenthesis (starting from the innermost)
2) Exponentials (left to right)
3) Multiplications and divisions (left to right)
4) Additions and subtractions (left to right)
5) Relational operators (==, ~=, >, >=, <, <=)
(left to right)
6) ~ operators
7) & and && operators (left to right)
8) |, ||, and xor operators (left to right)
17
Logical Functions
Matlab includes a number of logical functions,
which can be used with relational and logical
operators
Function Purpose
ischar(a) Returns true if a is a character array
isempty(a) Returns true if a is an empty array
isinf(a) Returns true if the value of a is Inf (infinite)
isnan(a) Returns true if the value of a is NaN (not a number)
isnumeric(a) Returns true if a is a numeric array
18
Examples
To count the occurrence of digits in a
sentence (represented by array B)
(B(i) >= '0') & (B(i) <= '9')
Examples
To determine if a quadratic equation
has two distinct real roots
(b^2 – 4*a*c) > 0