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

Illustration of indexing, Illustration of Indexing: Though, rather tha...

Illustration of Indexing: Though, rather than of creating the index vector manually as shown here, the process to initialize the index vector is use to a sort function. The al

Curve fitting, Curve Fitting: The data is fundamentally either discret...

Curve Fitting: The data is fundamentally either discrete or continuous. In most of the applications, data is sampled, for illustration, The temperature recorded every ho

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

Illustration of nested for loops and the if statements, Illustration of nes...

Illustration of nested for loops and the if statements: For illustration,when the file contains: 33   -11  2  4      5   9 22     5  -7  2     11    3 the outco

Bus311.., #qYou will need to examine two of the nine sections of data: one ...

#qYou will need to examine two of the nine sections of data: one section of qualitative data (choose either Gender or Position) one section of quantitative data (choose either Intr

While loops, WHILE Loops: The while statement is used as a conditional...

WHILE Loops: The while statement is used as a conditional loop in MATLAB; it is used to repeat an action whenever ahead of the time it is not known how many times the action w

Assignment , Determine sequence weights for the sequences ACTA, ACTT, CGTT,...

Determine sequence weights for the sequences ACTA, ACTT, CGTT, and AGAT in problem 1 by using Thompson, Higgins, and Gibson method a) compute pairwise distances between sequences

Vectors of nested structures, Vectors of nested structures: The Combin...

Vectors of nested structures: The Combining vectors and nested structures are possible to have a vector of structures in which several fields are structures themselves. Here i

Steps for input output functions - lower level file, Steps for input output...

Steps for input output functions - Lower level file: The steps involved are as shown below:  Open the file.  Read the file, write to the file, or append to the file.

Program of persistent variables, Program of Persistent variables: The ...

Program of Persistent variables: The program below elaborates this. The script calls a function func1 that initializes a variable count to 0, then increment it, and print the

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