Logical Operations In Matlab.

Slides:



Advertisements
Similar presentations
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Advertisements

Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
22/11/ Selection If selection construct.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Learning Javascript From Mr Saem
 Type Called bool  Bool has only two possible values: True and False.
Basic concepts of C++ Presented by Prof. Satyajit De
CGS 3066: Web Programming and Design Spring 2017
VB Script V B S.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Python: Control Structures
EGR 2261 Unit 4 Control Structures I: Selection
IF statements.
Selection Statements by Ahmet Sacan
JavaScript Syntax and Semantics
Topics The if Statement The if-else Statement Comparing Strings
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
MATLAB: Structures and File I/O
Introduction to MATLAB
Control Structures – Selection
PH2150 Scientific Computing Skills
Arrays, For loop While loop Do while loop
JavaScript conditional
Lecture 3 MATLAB programming (1)
Conditional Statements
Topics The if Statement The if-else Statement Comparing Strings
CMSC 120: Visualizing Information 3/25/08
Conditional Statements
Selection CSCE 121 J. Michael Moore.
Selection Statements Chapter 4 Attaway MATLAB 4E.
CMSC 202 Java Primer 2.
If selection construct
Chapter 7 Conditional Statements
If selection construct
Do … Loop Until (condition is true)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Three Special Structures – Case, Do While, and Do Until
Computer Science Core Concepts
Conditional Logic Presentation Name Course Name
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Relational Operators.
Selection Statements Chapter 3.
Decision making and control functions
The Selection Structure
Selection Statements Chapter 4 Attaway MATLAB 5E.
CGS 3066: Web Programming and Design Spring 2016
Lecture 9: JavaScript Syntax
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
CISC 110 Day 2 Programming Basics.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

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.