5.1 Relational and Logical Operators
5.1 Relational and Logical Operators
Chapter Five
Programing in MATLAB
RELATIONAL OPERATORS:
˃ Greater than
˂ Less than
== Equal to
~= Not Equal to
˃˃5˃8
ans=
˃ ˃ a=5 ˂ 8
a=
˃˃y=(6˂10)+(7˃8)+(5*3==60/4)
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
y=
˃˃d= c˃=b
d=
0 1 10110
˃˃b==c
ans=
001000
˃˃f=b-c˃0
f=
1 0 0 100 2
A=
2 9 4
-3 5 2
6 7 -1
˃˃B=A˂=2
B=
1 0 0
1 0 1
0 0 1
Logical operators:
• Logical operators have numbers as operands. A nonzero number is true, and a zero
number is false.
• Logical operators (like relational operator) are used as arithmetic operators within a
mathematical expression. The result can be used in other mathematical operation, in
addressing arrays, and together with other MATLAB command (e.g. if) to control the
flow of a program.
• Logical operators (like relational operator) can be used with scalars and arrays.
• Logical operators AND and OR can have both operands as scalar, arrayes or one array
and one scalar.
• Logical operator NOT has one operand. When it is used with a scalar the outcome is a
scalar 0 or 1
Following are some example:
˃˃ 3&7
ans = 3 and 7 both are (nonzerero),so outcome is 1
1
˃˃a=5|0
a=
1
˃˃~25
ans =
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
0
˃˃t=25*((12&0)+(`0)+(0|5))
t=
50
˃˃x=[9 3 0 11 0 15]; y=[2 0 13 -11 0 4];
˃˃ x&y
ans =
1 0 0 1 0 1
˃˃z=x|y
z=
1 1 1 1 0 1
˃˃~(x+y)
ans=
0 0 0 1 1 0
Order of precedence
Arithmetic, relation, and logical operators can be combined in mathematical expression.
When an expression has such a combination, the result depends on the order in which
the operations are carried out. The following is the order used by MATLAB:
Precedence operation
1 parentheses (if nested parentheses exit, inner ones have
precedence)
2 Exponention
3 Logical NOT(~)
4 Multiplication, division
5 Addition, subtraction
6 Relation operators (˃ ,˂,˂=,˃=,==,~=)
7 Logical AND (&)
8 Logical OR (|)
If two or more operations have the same precedence, the expression is executed in
order from left to right.
The following are examples of expressions that include arithmetic, relational, and
operators:
˃˃x=-2; y=5;
˃˃-5˂x˂-1
ans =
0
˃˃-5˂x & x˂-1
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
ans =
1
˃˃~(y˂7)
ans =
0
˃˃~y˂7
ans =
1
˃˃~((y˃=8)|(x˂-1))
ans =
1
MATLAB has built-in functions that are equivalent to the logical operators. These functions are:
not(A) equivalent to ~A
in addition MATLAB has other logical built-in functions, some of which are described in the
following table
˃˃xor(7,-5)
ans=
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
find(A˃d) 1 3 6
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
If A is a vector, ˃˃find(A˃10)
returns the address
ans=
of the element that
are large than d(any 3 6
relational operator
can be used )
The operations of the four logical operators, and, or, xor, and not can be summarized in a truth
table
(c) The days of month when the temperature was between 10 and 16.
Solution
T= [ 14 23 23 12 10 9 13 23 23 19 21 17 23 28 29 33 34 32 33 27 15 21 13 18 17 19 18
23 17 21 ]
Tabove24=T˃=24;
NdaysTabove24=sum(Tabove24)
NdaysThetween18and27=sum(Tbetween18and27)
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI
DatesTbetween10and16=find(T˃=10)&(T˂=16)