0% found this document useful (0 votes)
39 views

Matlab - CH4

The document discusses relational and logical operators in MATLAB. It defines relational operators such as less than, greater than, equal to and provides examples. It also defines logical operators such as AND, OR, NOT and provides a truth table. The order of precedence for operators is also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Matlab - CH4

The document discusses relational and logical operators in MATLAB. It defines relational operators such as less than, greater than, equal to and provides examples. It also defines logical operators such as AND, OR, NOT and provides a truth table. The order of precedence for operators is also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2nd Stage MATLAB Asst. Lec.

Anas Atilla

Relational and Logical Operators:

Relational and logical operators are operators that produce a true or false result. These
operators are very important, because they control which code gets executed in
MATLAB program.

1) Relational Operators:
A relational operator compares two numbers (e.g., 5 < 8) by determining
whether a comparison statement is true or false. If the statement is true, it is
assigned a value of 1. If the statement is false, it is assigned a value of 0.

Relational operators in MATLAB are:

Relational operator Description Example


>> 3 < 4 ↵
< Less than ans =
1
>> 4 <= 4 ↵
<= Less than or equal to ans =
1
>> 3 > 4 ↵
> Greater than ans =
0
>> 5>=4 ↵
>= Greater than or equal to ans =
1
>> 3 == 4 ↵
== Equal to ans =
0
>> 5~=4 ↵
~= Not Equal to ans =
1

73
2nd Stage MATLAB Asst. Lec. Anas Atilla

Notes:
 Equal to ( == ) relational operator consists of two= signs (with no space between
them), since one = sign is the assignment operator.
 There is no space between the relational operators that consist of two characters
(<=,>=, ~=).
 Relational operators are used as arithmetic operators within a mathematical
expression.

Example:

>> y=(6<10)+(7>8)+(5*3==60/4) ↵

y=
2

 Relational operators are also used with arrays (only arrays of the same size).
The comparison is done element-by-element, and the result is an array of the
same size with 1s and 0s which is called logical array and can be used for
addressing arrays.

Example:

>> r= [8 12 9 4 23 19 10] ↵
r=
8 12 9 4 23 19 10
>> s=r>=10 ↵
s=
0 1 0 0 1 1 1
>> t=r(s) ↵
t=
12 23 19 10

- Logical Operators:
A logical operator examines logical values and produces a result that is true (1)
or false (0) according to the specific operator.

74
2nd Stage MATLAB Asst. Lec. Anas Atilla

Logical operators in MATLAB are:

Logical
Form Description Example
operator
>> (3<5)&(7>10) ↵
AND A&B Operates on two operands (A,B). If
both are true, the result is true (1);
ans =
and (A,B) otherwise the result is false (0).
0
Operates on two operands (A,B). If >> (3<5) | (7>10) ↵
A|B
OR either one, or both, are true, the
result is true (1); otherwise (both ans =
or (A,B)
are false) the result is false (0). 1
Operates on one operand (A). Gives >> ~ (6<5) ↵
~ (A)
NOT the opposite of the operand; true (1)
if the operand is false, and false (0) ans =
not(A)
if the operand is true. 1

The operations of the three logical operators, and, or, and not can be summarized in a
truth table:

Input Output
A&B A|B ~ (A) ~ (B)
A B
and (A,B) or (A,B) not(A) not(B)
True True True True False False
Ture False False True False True
False Ture False True True False
False False False False True True

Order of precedence:
Arithmetic, relational, and logical operators can be combined in mathematical
expressions. 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:

75
2nd Stage MATLAB Asst. Lec. Anas Atilla

Precedence Operation

Parentheses.
1st (Highest)
For nested parentheses, the innermost are executed first.

2nd Exponentiation
3rd Logical NOT (~)
4th Multiplication, division
5th Addition and subtraction
6th Relational operators (<, <=, >, >=, ==, ~=)
7th Logical AND (&)
8th (Lowest) Logical OR ( | )

Note: If two or more operations have the same precedence, the expression is executed
in order from left to right.

Example:
>> ~ (5>=8 | -2<-1) ↵
ans =
0

>> ~(5>=8) | (-2<-1) ↵


ans =
1

>> (35/7>16/4)&~(3+5<=3^3) ↵
ans =
0

>> -5>-2 & 4==6 | -3<0 ↵


ans =
1

76

You might also like