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

5.1 Relational and Logical Operators

This document discusses relational and logical operators in MATLAB. It defines relational operators like greater than, less than, equal to, and not equal to. Logical operators like AND, OR, and NOT are also defined. Examples are provided to demonstrate how to use these operators with scalars, arrays, and in conditional expressions. Built-in logical functions equivalent to the logical operators are also described, such as AND, OR, and NOT. Finally, a sample problem demonstrates analyzing temperature data using relational and logical operators.
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)
67 views

5.1 Relational and Logical Operators

This document discusses relational and logical operators in MATLAB. It defines relational operators like greater than, less than, equal to, and not equal to. Logical operators like AND, OR, and NOT are also defined. Examples are provided to demonstrate how to use these operators with scalars, arrays, and in conditional expressions. Built-in logical functions equivalent to the logical operators are also described, such as AND, OR, and NOT. Finally, a sample problem demonstrates analyzing temperature data using relational and logical operators.
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/ 8

MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI

Chapter Five

Programing in MATLAB

5.1 RELATIONAL AND LOGICAL OPERATORS:

A relation operator compares tow 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 which is true (1) or false(0) according to the specific operator.

RELATIONAL OPERATORS:

Relational operators in MATLAB are:

Relational operator Description

˃ Greater than

˂ Less than

˂= Less than or equal to

˃= Greater than or equal to

== Equal to

~= Not Equal to

Some examples are:

˃˃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=

˃˃b=[15 6 9 4 11 7 14]; c=[8 20 9 2 19 7 10]

˃˃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]

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 in MATLAB are:


MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI

Logical operator Name Description

& AND Operates on two operands (A


and B).If both are true, the
Example : A&B
result is true(1), otherwise
the result is false(0)

| OR Operates on two operands (A


or B).If either 1 or both are
Example : A |B
true, the result is true(1),
otherwise(both are false) the
result is false(0)

~ NOT Operates on one operand


(A). GIf both are true, the
Example : ~A
result is true(1), otherwise
the result is false(0)

• 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

• Built- in logical functions:

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

and(A,B) equivalent to A&B

or(A,B) equivalent to A|B

not(A) equivalent to ~A

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

Function Description Example

Xor Exclusive or .Returns ˃˃xor(7,0)


true (1) if one
ans=
operand is true and
the other is false. 1

˃˃xor(7,-5)

ans=
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI

all(A) Returns 1(true) if all ˃˃A=[6 2 15 9 7 11]


elements in a vector
˃˃all(A)
A are true (nonzero)
ans=
Returns 0 (false) if
one or more 1
elements are
false(zero).if A is a ˃˃B=[6 2 15 9 0 11]
matrix treats a
˃˃all(B)
columns of A as
vectors, and returns ans=0
a vector with 1s and
0s.

any(A) Returns 1(true) if any ˃˃A=[6 0 15 0 0 11]


elements in a vector
˃˃any(A)
A is true (nonzero)
ans=
Returns 0 (false) if all
elements are 1
false(zero).if A is a
matrix treats a ˃˃B=[0 0 0 0 0 0]
columns of A as
˃˃any(B)
vectors, and returns
a vector with 1s and ans=
0s.
0

find(A) If A is a vector, ˃˃A=[6 0 15 0 0 11]


returns the indices of
˃˃find(A)
the nonzero
elements. ans=

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

Sample problem : Analysis of temperature data

The following were the daily maximum temperatures (in C ) 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

Use relational and logical operations to determine the following:

(a) The number of days that the temperature was above 24


(b) The number of days that the temperature was between 18 and 27

(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)

Tbetween18and27= (T˃=18)& (T˂=27);

NdaysThetween18and27=sum(Tbetween18and27)
MATLAB PR0GRAM FOR FUEL AND ENERGE DEPARTMENT BY ZAINAB YOUSIF AL-TIMARI

DatesTbetween10and16=find(T˃=10)&(T˂=16)

The script file (saved) is executed in command window:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy