If statement - matlab programming, MATLAB in Statistics

Assignment Help:

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


Related Discussions:- If statement - matlab programming

Example of median, Example of Median For the vector [1 4 5 9 12 33], th...

Example of Median For the vector [1 4 5 9 12 33], the median is the average of the 5 & 9 in the middle: >> median([1 4 5 9 12 33]) ans =     7

Median, Median The median is defined only for a data set which has been...

Median The median is defined only for a data set which has been sorted first, that means that the values are in order. The median of a sorted set of data values (n) is defined

Illustration of nested loops, Illustration of Nested loops: Running th...

Illustration of Nested loops: Running the script shows the output: >> printstars ***** ***** ***** The variable rows identifies the number of rows to print, and

Function sortrows - sorting strings, function sortrows - sorting strings: ...

function sortrows - sorting strings: The function sortrows sorts each and every row as a block, or group, and it also will work on numbers. Here in this illustration the rows

Run-time or execution-time error, Run-time or execution-time error: Th...

Run-time or execution-time error: The Run-time, or execution-time, errors are found whenever a script or function is executing. With most of the languages, an illustration of

Nested structures, Nested Structures: The nested structure is a struct...

Nested Structures: The nested structure is a structure in which at least one of the members is itself a structure. For illustration, a structure for the line segment may co

Functions which return values versus printing, Functions which return Value...

Functions which return Values versus printing: Also, the function which computes and returns values (throughout the output arguments) does not generally print them; that is le

Create a custom chi-square function in matlab, Introduction and Purpose ...

Introduction and Purpose In the lectures and tutorials you have been introduced to a number of probability distributions. You have also learned that modelling any system (such

Types of errors, Types of errors: There are some various kinds of erro...

Types of errors: There are some various kinds of errors which can occur in a program, that fall into the categories of run-time errors, syntax errors, and logical errors. T

Illustration of customizing plots, Illustration of Customizing plots: ...

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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd