What type of operator can be used to determine whether a specific relationship?

Vectors and Matrices

Stormy Attaway, in MATLAB [Fifth Edition], 2019

2.4.1 Relational Expressions With Vectors and Matrices

Relational operators can be used with vectors and matrices. For example, let's say that there is a vector vec, and we want to compare every element in the vector to 5 to determine whether it is greater than 5 or not. The result would be a vector [with the same length as the original] with logical true or false values.

>> vec = [5 9 3 4 6 11];

>> isg = vec > 5

isg =

 1 × 6 logical array

 0 1 0 0 1 1

Note that this creates a vector consisting of all logical true or false values. Although the result is a vector of ones and zeros, and numerical operations can be done on the vector isg, its type is logical rather than double.

>> doubres = isg + 5

doubres =

 5 6 5 5 6 6

>> whos

 Name Size Bytes Class Attributes

 doubres 1x6 48 double

 isg 1x6 6 logical

 vec 1x6 48 double

To determine how many of the elements in the vector vec were greater than 5, the sum function could be used on the resulting vector isg:

>> sum[isg]

ans =

 3

What we have done is to create a logical vector isg. This logical vector can be used to index into the original vector. For example, if only the elements from the vector that are greater than 5 are desired:

>> vec[isg]

ans =

 9 6 11

This is called logical indexing. Only the elements from vec for which the corresponding element in the logical vector isg is logical true are returned.

Quick Question!

Why doesn’t the following work?

 >> vec = [5 9 3 4 6 11];

 >> v = [0 1 0 0 1 1];

 >> vec[v]

 Array indices must be positive integers or logical values.

Answer:

The difference between the vector in this example and isg is that isg is a vector of logicals [logical 1s and 0s], whereas [0 1 0 0 1 1] by default is a vector of double values. Only logical 1s and 0s can be used to index into a vector. So, type casting the variable v would work:

 >> v = logical[v];

 >> vec[v]

 ans =

 9 6 11

To create a vector or matrix of all logical 1s or 0s, the functions true and false can be used.

>> false[2]

ans =

 0 0

 0 0

>> true[1,5]

ans =

 1 1 1 1 1

Beginning with R2016a, the ones and zeros functions can also create logical arrays directly.

>> logone = ones[1,5, 'logical']

logone =

 1 1 1 1 1

>> class[logone]

ans =

logical

Read full chapter

URL: //www.sciencedirect.com/science/article/pii/B9780128154793000027

Introduction to MATLAB

Stormy Attaway, in MATLAB [Fifth Edition], 2019

1.6 Relational Expressions

Expressions that are conceptually either true or false are called relational expressions; they are also sometimes called Boolean expressions or logical expressions. These expressions can use both relational operators, which relate two expressions of compatible types, and logical operators, which operate on logical operands.

The relational operators in MATLAB are:

OperatorMeaning
> greater than
= greater than or equals
> 3 < 5

ans =

 logical

 1

>> 2 > 9

ans =

 logical

 0

>> class[ans]

ans =

 'logical'

The type of the result is logical, not double. MATLAB also has built-in true and false.

>> true

ans =

 logical

 1

In other words, true is equivalent to logical[1] and false is equivalent to logical[0].

Starting in R2016b, the output in the Command Window shows a header for most classes except for the default number type double, char, and string. If the type of a number result is not the default of double, the type [or class] is shown above the resulting value, as in the underlined “logical” in the previous expressions. However, in order to save room, these types will frequently not be shown for the rest of the book. Note: the class names char and string are not shown because the class is obvious from the single quotes [for char] or double quotes [for string].

Although these are logical values, mathematical operations could be performed on the resulting 1 or 0.

>> logresult = 5 < 7

logresult =

 1

>> logresult + 3

ans =

 4

Comparing characters [e.g., ‘a’ < ‘c’] is also possible. Characters are compared using their ASCII equivalent values in the character encoding. So, ‘a’ < ‘c’ is a true expression because the character ‘a’ comes before the character ‘c’.

>> 'a' < 'c'

ans =

 1

The logical operators are:

OperatorMeaning
|| or
&& and
~ not

All logical operators operate on logical or Boolean operands. The not operator is a unary operator; the others are binary. The not operator will take a logical expression, which is true or false, and give the opposite value. For example, ~[3 < 5] is false as [3 < 5] is true. The or operator has two logical expressions as operands. The result is true if either or both of the operands are true, and false only if both operands are false. The and operator also operates on two logical operands. The result of an and expression is true only if both operands are true; it is false if either or both are false. The or/and operators shown here are used for scalars, or single values. Other or/and operators will be explained in Chapter 2.

The || and && operators in MATLAB are examples of operators that are known as short-circuit operators. What this means is that if the result of the expression can be determined based on the first part, then the second part will not even be evaluated. For example, in the expression:

2 < 4 || 'a' == 'c'

the first part, 2 < 4, is true so the entire expression is true; the second part ‘a’ == ‘c’ would not be evaluated.

In addition to these logical operators, MATLAB also has a function xor, which is the exclusive or function. It returns logical true if one [and only one] of the arguments is true. For example, in the following only the first argument is true, so the result is true:

>> xor[3 < 5, 'a' > 'c']

ans =

 1

In this example, both arguments are true so the result is false:

>> xor[3 < 5, 'a' < 'c']

ans =

 0

Given the logical values of true and false in variables x and y, the truth table [see Table 1.1] shows how the logical operators work for all combinations. Note that the logical operators are commutative [e.g., x || y is the same as y || x].

Table 1.1. Truth Table for Logical Operators

xy~ xx || yx && yxor[x,y]TrueTrueFalse
True False True True False
 False False True False True
False True False False False

As with the numerical operators, it is important to know the operator precedence rules. Table 1.2 shows the rules for the operators that have been covered thus far in the order of precedence.

Table 1.2. Operator Precedence Rules

OperatorsPrecedenceParentheses: [ ]Power ˆUnary: negation [-], not [~]Multiplication, division ⁎,/,\Addition, subtraction +, -Relational <, <=, >, >=, ==, ~=And &&Or ||
Highest
Lowest

Quick Question!

Assume that there is a variable x that has been initialized. What would be the value of the expression

 3 < x < 5

if the value of x is 4? What if the value of x is 7?

Answer:

The value of this expression will always be logical true, or 1, regardless of the value of the variable x. Expressions are evaluated from left to right. So, first the expression 3 < x will be evaluated. There are only two possibilities: either this will be true or false, which means that either the expression will have the logical value 1 or 0. Then, the rest of the expression will be evaluated, which will be either 1 < 5 or 0 < 5. Both of these expressions are true. So, the value of x does not matter: the expression 3 < x < 5 would be true regardless of the value of the variable x. This is a logical error; it would not enforce the desired range. If we wanted an expression that was logical true only if x was in the range from 3 to 5, we could write 3 < x && x < 5 [note that parentheses are not necessary].

Practice 1.3

Think about what would be produced by the following expressions, and then type them in to verify your answers.

 3 == 5 + 2

 'b' < 'a' + 1

 10 > 5 + 2

 [10 > 5] + 2

 'c' == 'd' - 1 && 2 < 4

 'c' == 'd' - 1 || 2 > 4

 xor['c' == 'd' - 1, 2 > 4]

 xor['c' == 'd' - 1, 2 < 4]

 10 > 5 > 2

Note: be careful about using the equality and inequality operators with numbers. Occasionally, roundoff errors appear, which means that numbers are close to their correct value but not exactly. For example, cos[pi/2] should be 0. However, because of a roundoff error, it is a very small number but not exactly 0.

>> cos[pi/2]

ans =

 6.1232e-17

>> cos[pi/2] == 0

ans =

 0

Read full chapter

URL: //www.sciencedirect.com/science/article/pii/B9780128154793000015

Packages and Use Clauses

Peter J. Ashenden, ... Darrell A. Teegarden, in The System Designer's Guide to VHDL-AMS, 2003

Example

A package that provides signed arithmetic operations on integers represented as bit vectors might include a relational operator, defined as shown in Figure 10-11. The function negates the sign bit of each operand, then compares the resultant bit vectors using the predefined relational operator from the package standard. The full selected name for the predefined operator is necessary to distinguish it from the function being defined. If the return expression were written as “tmp1 < tmp2”, it would refer to the function in which it occurs, creating a circular definition.

FIGURE 10-11. An operator function for comparing two bit vectors representing signed integers.

Read full chapter

URL: //www.sciencedirect.com/science/article/pii/B9781558607491500104

Generalizing the Algebraic Operators

C.J. Date, ... Nikos A. Lorentzos, in Time and Relational Theory [Second Edition], 2014

Exercises

11.1

In the body of the chapter we defined U_ versions of the regular relational operators, including operators like MATCHING that are fundamentally just shorthand. But what about the PACK and UNPACK shorthands? Would U_PACK and U_UNPACK operators make sense?

11.2

Does the following identity hold?

USING [ ACL ] : r1 INTERSECT r2 ≡

 USING [ ACL ] : r1 MINUS [ USING [ ACL ] : r1 MINUS r2 ]

11.3

Consider the operator U_JOIN. Assume for simplicity that the packing and unpacking are to be done on the basis of a single attribute A. Confirm that the following identity holds:

USING [ A ] : rl JOIN r2 ≡

 WITH [ tl := r1 RENAME { A AS X },

 t2 := r2 RENAME { A AS Y } ,

 t3 := t1 JOIN t2 ,

 t4 := t3 WHERE X OVERLAPS Y ,

 t5 := EXTEND t4 : { A := X INTERSECT Y } ,

 t6 := T5 { ALL BUT X , Y } ] :

 PACK t6 ON [ A ]

Confirm also that if r1 and r2 are initially packed on A, the final PACK step is unnecessary.

Read full chapter

URL: //www.sciencedirect.com/science/article/pii/B9780128006313500113

C Programming Essentials

Martin P. Bates, in Programming 8-bit PIC Microcontrollers in C, 2008

Conditional Operations

Where a logical condition is tested in a while, if, or for statement, relational operators are used. One variable is compared with a set value or another variable, and the block is executed if the condition is true. The conditional operators are shown in Table 2.7. Note that double equals is used in the relational test to distinguish it from the assignment operator.

Table 2.7. Conditional Operators

OperationSymbolExample
Equal to == if[a==0] b=b+5;
Not equal to != if[a != 1] b=b+4;
Greater than > if[a > 2] b=b+3;
Less than < if[a < 3] b=b+2;
Greater than or equal to >= if[a >= 4] b=b+1;
Less than or equal to <= if[a <= 5] b=b+0;

Sometimes, a conditional test needs to combine tests on several values. The tests can be compounded by using logical operators, as follows:

AND condition:if [[a>b]&&[c=d]]...OR condtion:if  [[a>b]||[c=d]]...

Read full chapter

URL: //www.sciencedirect.com/science/article/pii/B978075068960100002X

matlab Programming

James C. Squire P.E., Ph.D., Julie Phillips Brown Ph.D., in Programming for Electrical Engineers, 2021

Relational Expressions

A logical expression is a statement that evaluates to either “true” or “false.” Relational operators are a type of logical operator, and compare two values such as 5 > 4 [true] or 3 ≤ −4 [false]. matlab returns a 1 to indicate true and 0 to indicate false. matlab has several types of relational operators; some of the most common are listed below:

OperatorNameExampleExample result
> Greater than [5 > 2] 1
= 6 0
3

[c] x = -4

Chủ Đề