Programming in MATLAB for beginners

Programming in MATLAB for beginners

Relational Operators in MATLAB:

Function Description
 < Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
~= Not Equal to

Logical Operators in MATLAB:

Function Description
 & AND : if both are true, the result is true otherwise result is false
| OR: if either one or both are true , then result is true otherwise result is false
~ NOT: Returns true if operator is false and false if operator is true

MATLAB provides build-in function that are equivalent to the logical operators.

Function Description
 xor(a,b) Exclusive OR: Returns true if one operand is true and other is false
all(A) Returns true if all elements in vector A are true otherwise false
any(A) Returns true if any element in vector A is true or returns false if all elements are false
find(A) Returns the indices of nonzero elements in vector A
find(A>d) Returns the position of the elements that are larger than d in vector A

Order of Precedence:

Precedence Description
 1 (highest one) Parantheses
2 Exponentiation
3 Logical NOT
4 Multiplication, Division
5 Addition , Subtraction
6 Relational Operators
7 Logical AND
8 Logical OR

Conditional Statements

A conditional statements are commands in Matlab that allow  MATLAB program to make decision of whether to execute a group of commands that follow the conditional statement or to skip commands.

if-end in MATLAB:

if conditional expression
... ] group of commands
end

if-else-end in MATLAB:

if conditional expression
... ] group of commands
else
... ] group of commands
end

if-elseif-else-end in MATLAB:

if conditional expression
... ] group of commands
elseif conditional expression
... ] group of commands
else
... ] group of commands
end

switch-case Statement:

It is elternative of if-elseif-end in MATLAB, it provides help for choosing one group of code for several group of codes. The value of switch expression in the switch command is compared with the values that are next to each other of case statement. If match found the group of commands that follow the case statement with the match are executed. Only one group of commands is executed for each condition.

switch switch expression
case value1
... ] group of statements
case value2
... ] group of statements
otherwise
... ] group of statements
end

if no match is found and otherwise statement is present, then group of statements between otherwise and end are executed.

Loops

In a loop, the execution of command, or group of the commands, is repeated several times. Matlab has two kind of loops

for-end Loop:

for k = f : s : t
... ] group of commands
end

  • k is loop index variable
  • f is to initialize value of k
  • s is the increment in k after each iteration
  • t is the value of k in last iteration

while-end Loop:

while-end loop are used in situation when looping is needed, but number of iterations are not known in advance. In while loop the number of iterations are not specified when loop starts.

 
while conditional expession
... ] group of commands
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s