Anda di halaman 1dari 2

Logical Operators and Find

Logical operators are useful when writing batch files or custom functions. Logical operators,
when com-

bined with flow control, allow for complex choices to be compactly expressed.

11.1

>, >=, <, <=, ==, !=

The core logical operators are

Symbol

Function

Definition

>

greater

Greater than

>=

greater_equal

Greater than or equal to

<

less

Less than

<=

less_equal

Less than or equal to

==

equal

Equal to

!=

not_equal

Not equal to
Logical operators can be used on scalars, arrays or matrices. All comparisons are done
element-by-

elementandreturneither Trueor False. Forexample,suppose xand


yarearrayswhicharebroadcastable.

z= x < y will be an array of the same size as broadcast(x,y).shape composed of True and False.
Alter-

natively, if one is scalar, say y, then the elements of z are z[i,j] = x[i,j] < y. For instance, suppose

z = xLy where L is one of the logical operators above such as < or ==. The following table
examines the

behavior when x and/or y are scalars or arrays. Suppose z = x < y:

Scalar

Array

Scalar

Any

Any

z=x<y

zi j= x < yi j

Array

Any

Broadcastable

zi j= xi j< y

zi j= ˜ xi j< ˜ yi j

where ˜ x and ˜ y arethepost-broadcastingversionsof x and y.


Logicaloperatorsarefrequentlyusedinpor-

tions of programs known as flow control (e.g. if ... else ... blocks) which are be discussed in
Chapter

13. It is important to remember that array logical operations return arrays and that flow
control blo

Anda mungkin juga menyukai