If statement - matlab programming, MATLAB in Statistics

The IF Statement:

The if statement selects whether or not the other statement, or group of statements, is executed. The common form of the if statement is as shown below:

if condition

   action

end

The condition is a relational expression which is theoretically, or logically, either true or false. Action is a statement, or a group of statements, which will be executed when the condition is true. Whenever the if statement is executed, the condition is initially computed. If the value of the condition is theoretically true, the action will be executed, and if not, then the action will not be executed. The action can be several number of statements awaiting the reserved word end; the action is obviously bracketed by the reserved words if and end. (Note: This is dissimilar from the end which is used as an index into the vector or matrix.)

For illustration, the if statement below confirms to see whether the value of a variable is negative. When it is, the value changed to a positive number by using the absolute value function; or else nothing is changed.

if num < 0

num = abs(num)

end

When statements can be entered in the Command Window, though they usually make more sense in the scripts or functions. In the Command Window, if the line would be entered, then press the Enter key, then action, then Enter key, and finally end and Enter; the outcome will instantly follow. For illustration, the earlier if statement is shown twice here. Note that the output from the assignment is not suppressed; therefore the result of the action will be shown if the action is executed. At first time the value of the variable is negative therefore the action is executed and the variable is modified, but in the second situation the variable is positive therefore the action is skipped.

>> num = -4;

>> if num < 0

num = abs(num)

end

num =

4

>> num = 5;

>> if num < 0

num = abs(num)

end

>> 

This might be used, for illustration, to make sure that the square root function is not used on a negative number. The script below prompts the user for a number, and prints the square root. When the user enters a negative number, then the if statement changes it to positive before taking the square root.

1428_if statement.png

Posted Date: 10/19/2012 3:15:08 AM | Location : United States







Related Discussions:- If statement - matlab programming, Assignment Help, Ask Question on If statement - matlab programming, Get Answer, Expert's Help, If statement - matlab programming Discussions

Write discussion on If statement - matlab programming
Your posts are moderated
Related Questions
Example of Mode When no value appears more often than any other, the smallest value in the vector will be the mode of the vector. >> shortx = [2 5 1 4]; >> mode(shortx

Example of Nested functions: For illustration, the function below computes and returns the volume of a cube. Three arguments are then passed to it, for the width and length of

Example of Functions which complete a task: For illustration, the function below just prints the number arguments passed to it in a sentence format: As this function i

Generic code for Reading from Files: The generic code to complete this is as shown below:   fid = fopen('filename'); if fid == -1    disp('File open not suc

Location of a rectangle - graphics objects: The location of a rectangle is [x y w h], where x and y are the coordinates of the lower left point, here w is the width, and h is

Data structure layers: There are many layers in this variable. For illustration,  cyls is the whole data structure, that is a vector of structs  cyls(1) is a separate

Example of sorting strings: To sort a cell array of strings, also the sort function is used. The illustration is as follows, >> engcellnames = {'Chemical','Mechanical',.

MENU Function: The MATLAB also has a built-in function known as menu which will display a figure Window with push buttons for the choices. The very first string passed to the

Illustration of Customizing plots: Illustration, the bar and barh functions by the default place a width of 0.8 between bars. Whenever called as bar(x,y), the width of 0.8 is

Example of Functions which return values: Calling this function prints the circumference, since there is no way to store the value, hence, it can be used in following computat