Logical Operations In Matlab
Booleans Booleans are a class of variable that have values of 0 or 1. 0 = false 1 = true George Boole (1815-1864)
Booleans Using the code on the right, we can set the value of two variables (a and b) and then ask Matlab if they are equal to each other using “==“ Matlab returns 1 if they are equal and 0 if they are not.
Booleans In this case, a is not equal to b, so Matlab will return 0.
Booleans Similarly, you can ask Matlab whether one variable is greater than another using “>” Here Matlab will return 1
Booleans Here, Matlab will return 0
Booleans Similarly, the “<“ sign can be used to test whether one variable is less than another Here Matlab will return 1
Booleans Here Matlab will return 0
Booleans You can also compare two variables to see if one is greater than or equal to the other using “>=“ Here Matlab will return 1
Booleans Here Matlab will again return a 1
Booleans Here Matlab will return 0
Booleans Similarly, we can ask Matlab whether or not one variable is less than or equal to another using “<=“ Here Matlab will return 1
Booleans Here Matlab will also return 1
Booleans Here Matlab will return 0
Booleans Characters can also be compared using the above-mentioned operators.
Booleans For strings, using Boolean operators compares each element of the string and returns a 1 or 0 for each comparison.
Booleans If we want to know if the entire string for one variable is the same as for another variable then we need to use strcmp.
Booleans Using strcmp, if even one letter is different, it will return 0
Booleans Boolean operations also work on arrays and matrices.
Boolean Indexing You can also use Boolean variables to index other variables.
Boolean Indexing This can be a very nice shot- hand way to extract data of interest.
Boolean Indexing Boolean operations may be combined using “&”
Boolean Indexing Boolean operations may also be combined using | (for “or” statements).
if Statements If statements take Boolean arguments (e.g. a<b). If the Boolean is true, then the code after the if statement is executed.
if Statements If statements take Boolean arguments (e.g. a<b). If the Boolean is true, then the code after the if statement is executed. Otherwise Matlab checks to see if the elseif statement is true. If it is then Matlab executes the code immediately after the elseif statement. (You can have multiple elseif statements between the if and the else statement). Otherwise, the code after the “else” statement is executed.
if Statements Here’s an example with multiple elseif statements.
Switch Statements Switch statements take string arguments (e.g. ‘Tuesday’). For each case, Matlab compares the string beside the switch statement (our Day variable in this case) with the strings beside the case statements until it finds a match. Then it executes the code for the matching case. If no match is found, then it executes the code found under the otherwise statement.
While loops A while loop executes as long as its Boolean argument is true. In this example, the while loop will add 1 to n until n stops being less than 5.
For loops A for loops executes for a fixed number of times. In this case, it will cycle through 5 times and at each cycle tell us which number it is on.
Embedding Logical structures can be embedded within each other. Matlab automatically indents logical statements so that the start of the statement lines up with its end.