0% found this document useful (0 votes)
290 views3 pages

Experiment 5 Relational and Logical Operation

This document discusses relational and logical operations in MATLAB that allow for control of code execution based on true/false evaluations. Relational operations like equal, not equal, less than, greater than are used to compare values and return 1 for true and 0 for false. Logical operations like AND, OR, and NOT are used to combine multiple true/false evaluations. Examples demonstrate how to use these operations to find elements that meet certain criteria and count non-zero elements in an array. An exercise at the end tests the reader's understanding of applying these operations.

Uploaded by

kurddoski28
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)
290 views3 pages

Experiment 5 Relational and Logical Operation

This document discusses relational and logical operations in MATLAB that allow for control of code execution based on true/false evaluations. Relational operations like equal, not equal, less than, greater than are used to compare values and return 1 for true and 0 for false. Logical operations like AND, OR, and NOT are used to combine multiple true/false evaluations. Examples demonstrate how to use these operations to find elements that meet certain criteria and count non-zero elements in an array. An exercise at the end tests the reader's understanding of applying these operations.

Uploaded by

kurddoski28
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/ 3

University of Zakho Petroleum Engineering Department MATLAB Laboratory

______________________________________________________________________________________________

Experiment No. 5
Relational and Logical Operation
These operations and functions provide answers to True – False questions. One
important use of this capability is to control the flow or order of execution of a
series of MATLAB commands (usually in an M-file) based on the results of
true/false questions.

As inputs to all relational and logical expressions, MATLAB considers any


nonzero number to be true, and zero to be False. The output of all relational and
logical expressions produces one for True and zero for False, and the array is
flagged as logical. That is, the result contains numerical values 1 and 0 that can be
used in mathematical statement, but also ` allow logical array addressing.
Relational Operations

Operation Description
== Equal
~= Not equal
< Less than
> Greater than
<= Less than or equal
>= greater than or equal

(5 - 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Example:
» a = 1:9;
» b = 9-a;
» t = a > 4 %finds elements of (a) that are greater than 4.
t =
0 0 0 0 1 1 1 1 1
Zeros appear where a < 4, and ones where a > 4.

» t = (a==b) %finds elements of (a) that are equal to those in (b).


t =
0 0 0 0 0 0 0 0 0

LouicalOperation

Operation Description
& Logical AND
| Logical OR
~ Logical NOT
xor (a,b) Logical Exclusive OR

Example:
» a = [0 4 0 -3 -5 2];
» b = ~a
b =
1 0 1 0 0 0
» c = a & b
0 0 0 0 0 0
Example: Let x= [2 -3 5; 0 1 1 ] , then
a) Find elements in x that are greater than 2.
b) Find the number of nonzero elements in x

Solution
a)

(5 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
» x > 2
ans =
0 0 1
0 0 0
b)
» t = ~(~x);
»sum(sum(t))
ans =
5
Exercise:
1- If x= [8 3; 11 5], y = [12 1; 9 13], z = [-2 13; -5 1], what is the output of
the following statements:
a) v = x > y
b) w = z >= y
c) t = x & y < z

(5 - 3)

You might also like