Representing logical true and false, MATLAB in Statistics

Representing Logical true and False:

It has been stated that expressions which are theoretically true really have the integer value of 1, and expressions which are theoretically false really have the integer value of 0.Exhibiting the concepts of logical true and false in the MATLAB is somewhat different: the concept of false is presented by the integer value of 0, but the concept of true can be presented by any non-zero value (not merely the integer 1). This can lead to a few strange Boolean expressions. For illustration, consider the following if statement:

>> if 5

disp('Yes, this is true!')

end

Yes, this is true!

As 5 is a nonzero value, it is a way of telling true. And hence, when this Boolean expression is computed, it will be true, therefore the disp function will be executed and "Yes, this is true" is exhibited. Obviously, this is a pretty bizarre if the statement, one that hopefully would not ever be encountered!

Though, a simple mistake in an expression can lead to this type of answer. For illustration, let's state that the user is prompted for the choice of Y or N for a yes/no question:

letter = input('Choice (Y/N): ','s');

In the script we might want to execute a specific action if the user responded with 'Y'. Nearly all scripts would permit the user to enter either lower- or uppercase (example, either 'y' or 'Y') to specify yes. The appropriate expression that would return true if the value of letter was 'y' or 'Y' would be

letter == 'y' || letter == 'Y'

Though, if by fault this was written as:

letter == 'y' || 'Y'

this expression would  forever be true, regardless of the value of the variable letter. This is as 'Y' is a nonzero value; therefore it is a true expression. The first section of the expression might be false, but as the second expression is true the whole expression would be true.

Posted Date: 10/19/2012 3:17:47 AM | Location : United States







Related Discussions:- Representing logical true and false, Assignment Help, Ask Question on Representing logical true and false, Get Answer, Expert's Help, Representing logical true and false Discussions

Write discussion on Representing logical true and false
Your posts are moderated
Related Questions
Mode The mode of a data set is the value which appears most often. The built-in function in a MATLAB for this is known as the mode. >> x = [9 10 10 9 8 7 3 10 9 8 5 10];

Sorting Strings: For the matrix of strings, the sort function works exactly as formerly for numbers. For illustration, >> words = char('Hi', 'Hello', 'Howdy', 'Goodbye', '

Function Stubs: The other common debugging method, which is used when there is a script main program which calls many functions, is to use the function stubs. A function stub

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

Example of customizing plots: As the other illustration of customizing plots, the pieces of a pie chart can be "exploded" from the rest. In this situation, the two vectors are

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

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

Advanced File Input and Output: In that section, we saw how to read the values entered by user using the input as well as the output functions disp and fprintf, that shows inf

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

Logical Built-In Functions: There are built-in functions in the MATLAB which are useful in conjunction with vectors or matrices of all logical true or false values; two of the