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

Lab Session 08: Relational and Logical Operators

The document discusses relational and logical operators in MATLAB. Relational operators compare values and return 1 for true and 0 for false. Logical operators examine true/false statements and return 1 or 0. Relational and logical operators can be combined and used to control program flow. The order of operations is arithmetic, relational, logical from left to right with equal precedence. Built-in functions like AND, OR, and NOT perform logical operations. Practice problems apply relational and logical operators and functions to vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Lab Session 08: Relational and Logical Operators

The document discusses relational and logical operators in MATLAB. Relational operators compare values and return 1 for true and 0 for false. Logical operators examine true/false statements and return 1 or 0. Relational and logical operators can be combined and used to control program flow. The order of operations is arithmetic, relational, logical from left to right with equal precedence. Built-in functions like AND, OR, and NOT perform logical operations. Practice problems apply relational and logical operators and functions to vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Session 08

Relational and Logical Operators


A relational operator compares two numbers by determining whether a comparison
statement (e.g., 5 < 8) 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. A logical operator examines true/false statements and
produces a result that is true (1) or false (0) according to the specific operator. For example, the
logical AND operator gives 1 only if both statements are true. Relational and logical operators can
be used in mathematical expressions and, as will be shown in this chapter, are frequently used in
combination with other commands to make decisions that control the flow of a computer program.
Relational Operators

Relational operators in MATLAB are:

Note that the "equal to" relational operator consists of two= signs (with no space between
them), since one = sign is the assignment operator. In other relational operators that consist of two
characters, there also is no space between the characters (<=,>=, ~=).
 Relational operators are used as arithmetic operators within a mathematical expression.
The result can be used in other mathematical operations, in addressing arrays, and together
with other MATLAB commands (e.g., if) to control the flow of a program.
 When two numbers are compared, the result is 1 (logical true) if the comparison, according
to the relational operator, is true, and 0 (logical false) if the comparison is false.
 If two scalars are compared, the result is a scalar 1 or 0. If two arrays are compared (only
arrays of the same size can be compared), the comparison is done element-by-element, and

1
the result is a logical array of the same size with 1s and 0s according to the outcome of the
comparison at each address.
 If a scalar is compared with an array, the scalar is compared with every element of the
array, and the result is a logical array with 1s and 0s according to the outcome of the
comparison of each element.
 The results of a relational operation with vectors, which are vectors with 0s and 1s, are
called logical vectors and can be used for addressing vectors. When a logical vector is used
for addressing another vector, it extracts from that vector the elements in the positions
where the logical vector has 1s.
 Numerical vectors and arrays with the numbers 0s and 1s are not the same as logical vectors
and arrays with 0s and 1 s. Numerical vectors and arrays cannot be used for addressing.
Logical vectors and arrays, however, can be used in arithmetic operations. The first time a
logical vector or an array is used in arithmetic operations it is changed to a numerical vector
or array.
 Order of precedence: In a mathematical expression that includes relational and arithmetic
operations, the arithmetic operations (+,-,*, /,\) have precedence over relational operations.
The relational operators themselves have equal precedence and are evaluated from left to
right. Parentheses can be used to alter the order of precedence.
Logical Operators

Logical operators in MATLAB are:

2
 Logical operators have numbers as operands. A nonzero number is true, and a zero number
is false.
 Logical operators (like relational operators) are used as arithmetic operators within a
mathematical expression. The result can be used in other mathematical operations, in
addressing arrays, and together with other MATLAB commands (e.g. , if) to control the
flow of a program.
 Logical operators (like relational operators) can be used with scalars and arrays.
 The logical operations AND and OR can have both operands as scalars, both as arrays, or
one as an array and one as a scalar. If both are scalars, the result is a scalar 0 or 1. If both
are arrays, they must be of the same size and the logical operation is done element-by-
element. The result is an array of the same size with 1s and 0s according to the outcome of
the operation at each position. If one operand is a scalar and the other is an array, the logical
operation is done between the scalar and each of the elements in the array and the outcome
is an array of the same size with 1 s and 0s.
 The logical operation NOT has one operand. When it is used with a scalar, the outcome is
a scalar 0 or 1. When it is used with an array, the outcome is an array of the same size with
0s in positions where the array has nonzero numbers and 1 s in positions where the array
has 0s.
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:

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

Built-in Logical Functions


MATLAB has built-in functions that are equivalent to the logical operators. These functions are:

In addition, MATLAB has other logical built-in functions, some of which are described in the
following table:

4
Practice Exercise
1. Evaluate the following expressions without using MATLAB. Check the answers with
MATLAB.

2. Given: v = [4 -1 2 3 1 -2 5 0] and u = [5 -1 0 3 -3 2 1 5]. Evaluate the following expressions


without using MATLAB. Check the answers with MATLAB.

3. Evaluate the following expressions without using MATLAB. Check the answers with
MATLAB

You might also like