Difference between revisions of "Matlab Logic"

From Sutherland_wiki
Jump to: navigation, search
(New page: == Introduction == <!-- Often in an algorithm we must take a different path or do some extra work if some condition is true. Consider the following example: The [http://en.wikipedia.org/...)
 
m (If/Then Statements)
 
Line 8: Line 8:
  
 
== If/Then Statements ==
 
== If/Then Statements ==
{{Stub|section}}
 
  
The basic syntax is:
+
The basic syntax for <tt>if</tt> statements in Matlab is:
<source lang="matlab">
+
{| border="1" cellspacing="0" cellpadding="10"
if (condition1)
+
|- valign="top"
  % do some work
+
|<source lang="matlab">
elseif (condition2)
+
if (condition1)
  % do different work
+
  % do some work
  .
+
elseif (condition2)
  .
+
  % do different work
  .
+
.
else
+
.
  % do default work
+
.
end
+
else
 +
  % do default work
 +
end
 
</source>
 
</source>
 +
|<source lang="matlab">
 +
if (condition1)
 +
  % do some work
 +
elseif (condition2)
 +
  % do different work
 +
end
 +
</source>
 +
|<source lang="matlab">
 +
if (condition)
 +
  % do some work
 +
else
 +
  % do default work
 +
end
 +
</source>
 +
|<source lang="matlab">
 +
if (condition)
 +
  % do some work
 +
end
 +
</source>
 +
|}
 +
  
  
Line 136: Line 158:
  
 
|}
 
|}
 
 
 
  
 
== Switch Statements ==
 
== Switch Statements ==

Latest revision as of 09:13, 22 September 2008

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

Warn.jpg
This section is a stub and needs to be expanded.
If you can provide information or finish this section you're welcome to do so and then remove this message afterwards.

The basic syntax of the switch statement in Matlab is:

  switch ( variable )
  case { case1 case 2 }
    % statements...
  case case3
    % statements...
  otherwise
    % default statements
  end