Logical operators are used in portions of programs known as flow control (for example if . . . else

. . . end blocks). Vector or matrix logical operations return vector or matrix output and that flow control blocks require scalar logical expressions.

# Operators

**>** Greater than

**>=** Greater or equal to

**<** Less than

**<=** Less or equal to

**==** Equal to

**~=** Not equal to

Logical operators can operate on scalars, vector or matrices, all comparisons are done element-by element. Operator return either 1 (logical true) or 0 (logical false).

## & (AND), | (OR) and ~ (NOT)

Logical expressions can be combined using three logical devices,

**&** AND

**j** OR

**~** NOT

Recall that ~ (NOT) has higher precedence than & (AND) and j (OR). They follow the same as other logical operators. If used on two matrices, the dimensions must be the same. If used on a scalar and a matrix, the effect is the same as calling the logical device on the scalar and each element of the matrix.

## logical

It is use to convert non-logical elements to logical that take up only 1 byte of memory instead of 8 bytes.

In certain situations, a logical value is required, for instance by indexing an array we can access to the elements of

a matrix x x(#) where # can be a vector of indices. If # is not a number but a logical value, this

behavior changes.

>> x = [1 2 3 4]; >> y = [1 1]; >> x(y) ans = 1 1 >> y = logical([1 1]); >> x(y) ans = 1 2 >> y = logical([1 0 1 0]); >> x(y) ans = 1 3

It forces MATLAB to interpret the indices as indicator variables when deciding what to return, logical turns any non-zero value into logical true (1).

>> x = [1 2 3 4]; >> y = x> x(y) ans = 1 2 >> y ans = 1 1 0 0

MATLAB will generate a warning if the values differ from 0 or 1.

>> x=[0 1 2 3] x = 0 1 2 3 >> logical(x) Warning: Values other than 0

## find

find it takes logical inputs and returns matrix indices where the logical statement is true (usuful for multiple data series).

indices = find (x < y)

will return indices (1,2,. . .,numel(x))

while

[i,j] = find (x < y)

will return pairs of matrix indices what correspond to the places where x < y.

>> x = [1 2 3 4]; >> y = x> find(y) ans = 1 2 >> x = [1 2 ; 3 4]; >> y = x> find(y) ans = 1 2 3 >> [i,j] = find(y) i = 1 2 1 i = 1 1 2

## is*

It has a number of special purpose for logical tests to determine if a matrix has special characteristics.

Some operate element-by-element and produce a matrix of the same dimension as the input matrix while other produce only scalars. All begin with is.

**isnan** 1 if NaN element-by-element

**isinf** 1 if Inf element-by-element

**isfinite** 1 if not Inf element-by-element

**isreal** 1 if not complex valued. scalar

**ischar** 1 if input is a character array scalar

**isempty** 1 if empty scalar

**isequal** 1 if all elements are equal scalar

**islogical** 1 if input is a logical matrix scalar

**isscalar** 1 if scalar scalar

**isvector** 1 if input is a vector (1 x K of K x 1) scalar

For more information and logic functions search for is* in the help file.

## Exercises

- Load the “Returns” matrix created previously, count the number of negative returns for each asset.
- For each series, create an indicator variable that takes the value 1 is the return is larger than 2 by standard deviations or smaller than -2 by standard deviations. What is the average return conditional on falling into this range for both returns?
- Construct an indicator variable that takes the value of 1 when all returns are negative. Compute the correlation of the returns conditional on this indicator variable. How does this compare to the correlation of all returns?