Booleans Booleans are a class of variable that have values of 0 or 1 0 false 1 true George Boole 18151864 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 ID: 635641
Download Presentation The PPT/PDF document "Logical Operations In Matlab" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Logical Operations
In MatlabSlide2
Booleans
Booleans are a class of variable that have values of 0 or 1.
0 = false
1 = true
George Boole
(1815-1864)Slide3
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.Slide4
Booleans
In this case,
a
is not equal to b, so Matlab will return 0.Slide5
Booleans
Similarly, you can ask Matlab whether one variable is greater than another using “>”
Here Matlab will return 1Slide6
Booleans
Here, Matlab will return 0Slide7
Booleans
Similarly, the “<“ sign can be used to test whether one variable is less than another
Here Matlab will return 1Slide8
Booleans
Here Matlab will return 0Slide9
Booleans
You can also compare two variables to see if one is greater than or equal to the other using “>=“
Here Matlab will return 1Slide10
Booleans
Here Matlab will again return a 1Slide11
Booleans
Here Matlab will return 0Slide12
Booleans
Similarly, we can ask Matlab whether or not one variable is less than or equal to another using “<=“
Here Matlab will return 1Slide13
Booleans
Here Matlab will also return 1Slide14
Booleans
Here Matlab will return 0Slide15
Booleans
Characters can also be compared using the above-mentioned operators.Slide16
Booleans
For strings, using Boolean operators compares each element of the string and returns a 1 or 0 for each comparison.Slide17
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
.Slide18
Booleans
Using
strcmp
, if even one letter is different, it will return 0Slide19
Booleans
Boolean operations also work on arrays and matrices.Slide20
Boolean Indexing
You can also use Boolean variables to index other variables.Slide21
Boolean Indexing
This can be a very nice shot-hand way to extract data of interest.Slide22
Boolean Indexing
Boolean operations may be combined using “&”Slide23
Boolean Indexing
Boolean operations may also be combined using | (for “or” statements).Slide24
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.Slide25
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.Slide26
if Statements
Here’s an example with multiple
elseif
statements.Slide27
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.Slide28
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.Slide29
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.Slide30
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.