Showing posts with label Matlab Operators. Show all posts
Showing posts with label Matlab Operators. Show all posts

10/02/2015

If statement is a branch that uses a control expression/statement which in turn permits us to select and execute specific block/blocks of code while skipping other block/blocks of code.

The if-construct has the following format:

if  control/logical_expression_1
    {
        ........... execute this block 1
        ........... execute this block 1
      }

elseif  control/logical_expression_2
      {
        ........... execute this block 2
        ........... execute this block 2
      }

else 
{
        ........... execute this block 3
        ........... execute this block 3
      }
 end

Some important rules/points about if-construct:

1-if statement is must to use and can occur only once i.e at the beginning.It also tells the position counter or controller that the if-construct has been started.
2-else statement may or may not occur according to our demand but it can occur only once i.e before  the end statement, if the if statements are not nested.
3-elseif statement can occur as many times as we require it.It will always occur between if and end statements.

How these statements work?

There is a simple reason behind working of these if, elseif and else statements.That will be explained in the following paragraph.
These if and elseif statements require a logical 1(in machine language it's mean true) for their code block to be executed and a logical 0(in machine language it's mean fasle) for their code block not to be executed.
These logical 1's and 0's are provided by control/logical expressions,these logical/control expressions are combinations of relational and logic operators which forms a condition, if this applied condition becoms true result will be logical 1 and if this a applied condition becomes false result will be logical 0, which will control the execution and non execution of code block related to if and elseif statements respectively.This is how these statements work basically.

Note: else statement don't require a control/logical expression.

If none of the code block is executed above else statement, then code block related to else statement is executed by program counter, if this statement and code block exists.

Where the use of end statement is just to mark the boundary or finish point of the if-construct.To tell the position counter or controller that after the execution of end statement the controller will be out of the if construct.

08/07/2014

Colon Operator è :
It is easy to create small matrixes, row vectors and column vectors but what happens when these array contain hundreds of values, it’s just practically not possible to write each value, for these MATLAB provides us with a special strong operator i.e Colon operator, represented as :
           It is used to declare a large range of values with some increment(according to demand) and correspondingly large ranged row vector, column vector and a matrix can be created using this strong operator i.e colon operator.
General form of colon operator,  INITIALVALUE:INCREMENT:FINALVALUE
By default increment is 1 if we will not mention increment to MATLAB for this we can also use the general form as INITIALVALUE:FINALVALUE, but we can set increment according to our demand for this we have to use the general form of colon operator as discussed above.
 ÄFor example we set:
      Initial value=1|  Increment=1 |  Final value=10
      So from initial to final value step by step increment will be like this described below.
                 Use of Colon Operator in MATLAB as a shortcut key.

                   
 ÄMATLAB declaration and result of colon operator :
%declaration of row vector using colon operator.
x=1:15 % here by default increment will be 1,we can also declare it as x=[1:15].
b=1:0.5:15 % here have limited MATLAB to take an increment of 0.5 each time, till final value.

%declaration of column vector using colon operator.
c=[1:15]' % by default increment is 1.
d=[1:0.5:15]'% MATLAB become limited to take an increment of 0.5 each time

%matrix declaration using colon operator.
g=[d d] % it will generate 30*2 matrix.

? To download above script click below:

script m-file for matrix,row vector and column vector declaration using colon operator

Blog Archive

Popular Posts