Logical Operators

MATLAB offers three types of logical operators and functions:

Element-wise -- operate on corresponding elements of logical arrays.

Bit-wise -- operate on corresponding bits of integer values or arrays.

Short-circuit -- operate on scalar, logical expressions.

The values returned by MATLAB logical operators and functions, with the exception of bit-wise functions, are of type logical and are suitable for use with logical indexing.

 

Element-Wise Operators and Functions

The following logical operators and functions perform element-wise logical operations on their inputs to produce a like-sized output array. The examples shown in the following table use vector inputs A and B, where

A = [0 1 1 0 1];

B = [1 1 0 0 1];

ele

For operators and functions that take two array operands, (&, |, and xor), both arrays must have equal dimensions, with each dimension being the same size. The one exception to this is where one operand is a scalar and the other is not. In this case, MATLAB tests the scalar against every element of the other operand.

 

Note MATLAB converts any finite nonzero, numeric values used as inputs to logical expressions to logical 1, or true.

 

Operator Overloading.   You can overload the &, |, and ~ operators to make their behavior dependent upon the data type on which they are being used. Each of these operators has a representative function that is called whenever that operator is used. These are shown in the table below

elee

posted on 2010-11-23 20:57  尛尛蟲  阅读(451)  评论(0)    收藏  举报