What Is a Logical Type in MATLAB?
Matlab is a mathematical and engineering modeling tool that can carry out literally thousands of operations, from simple arithmetic to advanced differential equations. Logic deals with properties that can exist in only one of two possible states, such as on or off, yes or no, true or false, or in the context of computers, one or zero. In Matlab, a number, variable or matrix is of the logical data type if its value or values are entirely ones and/or zeros and Matlab created them from a logical function or operator.
-
Logical Operators
-
Many logical operators Matlab uses exist when making comparisons. For example, if you type at Matlab's command prompt "50>40" (without quotes) and hit Enter, Matlab will return a one. If you type "2 == 3" (querying whether two is equal to three), Matlab will return a zero. These and other inequality operators are a small component of Matlab's logical operators.
Logical Functions
-
Certain built-in functions in Matlab check whether a condition is true or false, and then return a one or zero, respectively. If the function begins with "is" then its output is a logical data type and the function is a logical functions. For example, the function "ischar" will return a one if you give it a string of only letters or a zero if you give it any numbers. Similarly, the function "isfinite" returns a zero if you pass it as an argument whose value is infinity.
-
Islogical Function
-
The islogical function is also a logical function and will tell you right away if a number, variable, matrix or array consists of only ones and zeros that were created by a logical operator or function. Typing "islogical([0 1 0 1 0 1 1 0 0])" will return a one only if you created those values by using logical operators or functions. For distinction, note this example using a variable: If you type "q=1" and then "islogical(q)" then you will get a zero. If you type "q = 50>40" and then "islogical(q)" you will get a one.
Class Function
-
The class function tells you the class, or data type, of the number or variable you give it. This is a quick way to explicitly determine if a number is of the logical data type. Using the same example in the previous section, if you type "q=1" and then "class(q)" then Matlab will tell you its class is "double," another class of data types in Matlab. Conversely, if you type "q = 50>40" and then "class(q)" then Matlab will output "logical."
-