Matlab Logic
From Sutherland_wiki
Contents
Introduction
If/Then Statements
The basic syntax for if statements in Matlab is:
if (condition1)
% do some work
elseif (condition2)
% do different work
.
.
.
else
% do default work
end

if (condition1)
% do some work
elseif (condition2)
% do different work
end

if (condition)
% do some work
else
% do default work
end

if (condition)
% do some work
end

Relational Operators
Operator  Description  Example 

a == b  Compares a and b and returns "true" if they are equal. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 == 3); % comp is "false", comp=0
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a==b; % c=[0 0; 1 0]

a ~= b  Compares a and b and returns "true" if they are NOT equal. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 ~= 3); % comp is "true", comp=1
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a~=b; % c=[1 1; 0 1]

a > b  Returns "true" if a is greater than b. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 > 3); % comp is "true", comp=1
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a>b; % c=[0 0; 0 1]

a < b  Returns "true" if a is less than b. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 < 3); % comp is "false", comp=0
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a<b; % c=[1 1; 0 0]

a >= b  Returns "true" if a is greater than or equal to b. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 >= 3); % comp is "false", comp=0
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a>=b; % c=[0 0; 1 1]

a <= b  Returns "true" if a is less than or equal to b. If a and b are arrays, this returns an array that provides the comparison of each entry in a and b  comp = (5 <= 3); % comp is "true", comp=1
a = [1 2; 3 5];
b = [2 4; 3 4];
c = a<=b; % c=[1 1; 1 0]

Logical Operators
Operator  Description  Example 

a & b  Returns true if BOTH a AND b are true.  a=5; b=4; c=6;
d = (a>b) & (c>b); % true
e = (a>b) & (c<b); % false
f = (a<b) & (c<b); % false
g = (a>b) & (c>b) & a+b>c; % true.

a  b  Returns true if EITHER a OR b are true.  a=5; b=4; c=6;
d = (a>b)  (c>b); % true
e = (a>b)  (c<b); % true
f = (a<b)  (c<b); % false
g = (a>b)  (c>b)  a+b>c; % true
h = a>b & b>c  c>a; % true

~a  "NOT" operator. You have already seen this in ~=. Returns true if a is false. Otherwise returns false.  a=5; b=4; c=6;
d = ~(a>b); % false
e = ~(a>b); % false

Switch Statements

The basic syntax of the switch statement in Matlab is:
switch ( variable )
case { case1 case 2 }
% statements...
case case3
% statements...
otherwise
% default statements
end