# Intro to MATLAB Unit 4: LOGICALS AND RANDOM NUMBERS

## Relational Operators

Think about inequalities from math class.

> - greater than < - less than == - equal to ~= - not equal to >= - greater than or equal to <= - less than or equal to

The result of these operations will be either 1 or 0, 1 is true and 0 is false:

```8 >= 0

4 == 3

4 ~= 3

9 * -4 == 12 - 48

%Note that the arithmetic operations happened before the comparison.
```

Variables can (and often will) be used with these operators as well.

When comparing vectors or matrices, the result will be a vector or matrix with ones and zeros (they must be the same size in order to be compared):

```x = 1:10
y = [4 1 9 3 8 5 7 10 6 2]

x >= y

y == x

%Comparing a scalar to a vector acts as an element by element operation:
6 == x

s = y <= 4

%These result vectors can be useful: plug them back in as an address and you have
%all of the values that match the comparison:

t = y(s)

%There's a special name for these vectors: they are called logical vectors.
%They are not the same as a normal vector with 1s and 0s. For example:

s2 = [1 1 0 1 0 0 0 0 0 1];
t = y(s2)

whos
```

## Truth tables

There are also what's called logical operators: AND, OR, and NOT

```%%%%%%%
% AND %
%%%%%%%

% 1 && 1 = 1
% 1 && 0 = 0
% 0 && 1 = 0
% 0 && 0 = 0

%%%%%%
% OR %
%%%%%%

% 1 || 1 = 1
% 1 || 0 = 1
% 0 || 1 = 1
% 0 || 0 = 0

%%%%%%%
% NOT %
%%%%%%%

% ~1 = 0
% ~0 = 1
```

When using scalars with the logical operators, everything besides zero is considered to be "true"

```3 && 7

-2 && 100

0 && 4

0 || -11

~75

~(34 * 45 * 0)
```

## Order of operations

NOT is performed before any arithmetic (outside of parentheses), but relational or logical operations are performed last:

```x = 10;
y = -5;
z = 19.2;

a = 5 && ~(5 || ~2) + (0 && -10)
b = x * y > z || (y - z) >= -2*x
c = x && ~y || ~(z && (y^2 < z))

%As always, using parenthesis to clarify your meaning is usually a good
%idea.
```

## Some more useful logical commands

The find function returns the indices of nonzero numbers. It can be used with relational or logical operators to find the location of values that fit a certain criteria:

```y = 1:10;

find(y)

y(3:6) = [0 0 0 0];

find(y)

s = find(y>4)
y(s)
```

The strcmp function compares string values, if they match it returns a 1 (true) otherwise it returns a zero (false).

```strcmp('Anna','Anna')
strcmp('Anna','Ana')
strcmp('Anna','anna')   % case matters!

%You can also use the strcmp command with variables (as long as the
%variables contain strings!):
x = 'gouda'
y = 'gouda'

strcmp(x,y)

%Mixing inputs -- one string, one variable:
strcmp(x,'cheddar')
```

## Random number generation

Important for many computations and simulations.

rand has many different uses!

```%generate a number between 0 and 1:
rand

%Generating vectors of numbers between 0 and 1:
rand(1, 7)
rand(1, 3)

%Generating a random square matrix:
rand(3)
rand(4)

%rand(m, n) generates a random m x n matrix:
rand(2, 4)
rand(4, 5)

%You can specify to have a certain range by multiplying and adding an offset:

%5 random numbers from -10 to 15:
25*rand(1,5) - 10

%10 random numbers from 50 to 100:
50*rand(1,10) + 50
```

randi generates random integers:

```randi([2 10])   % give a range of numbers
randi(100)      % limit the maximum number

%set the minimum:
randi([50 100], 4, 5)
```

randn generates a normal distribution with mean of 0 and std of 1.

```%Usedjust like rand:
randn(2, 4)
randn(3)

%For specific distributions, multiply by the desired standard deviation and
%add the desired mean:

%std of 5, mean of -50:
5*randn(1,5)-50

%std of 10, mean of 70:
10*randn(1,10)+70
```

Lecture notes for CSE200 (Fall 2015) at Washington University in St. Louis by Marion Neumann (based on materials from Doug Shook).