Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logical Operations In Matlab.

Similar presentations


Presentation on theme: "Logical Operations In Matlab."— Presentation transcript:

1 Logical Operations In Matlab

2 Booleans Booleans are a class of variable that have values of 0 or 1.
0 = false 1 = true George Boole ( )

3 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.

4 Booleans In this case, a is not equal to b, so Matlab will return 0.

5 Booleans Similarly, you can ask Matlab whether one variable is greater than another using “>” Here Matlab will return 1

6 Booleans Here, Matlab will return 0

7 Booleans Similarly, the “<“ sign can be used to test whether one variable is less than another Here Matlab will return 1

8 Booleans Here Matlab will return 0

9 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

10 Booleans Here Matlab will again return a 1

11 Booleans Here Matlab will return 0

12 Booleans Similarly, we can ask Matlab whether or not one variable is less than or equal to another using “<=“ Here Matlab will return 1

13 Booleans Here Matlab will also return 1

14 Booleans Here Matlab will return 0

15 Booleans Characters can also be compared using the above-mentioned operators.

16 Booleans For strings, using Boolean operators compares each element of the string and returns a 1 or 0 for each comparison.

17 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.

18 Booleans Using strcmp, if even one letter is different, it will return 0

19 Booleans Boolean operations also work on arrays and matrices.

20 Boolean Indexing You can also use Boolean variables to index other variables.

21 Boolean Indexing This can be a very nice shot- hand way to extract data of interest.

22 Boolean Indexing Boolean operations may be combined using “&”

23 Boolean Indexing Boolean operations may also be combined using | (for “or” statements).

24 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.

25 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.

26 if Statements Here’s an example with multiple elseif statements.

27 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.

28 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.

29 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.

30 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.


Download ppt "Logical Operations In Matlab."

Similar presentations


Ads by Google