Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.

Slides:



Advertisements
Similar presentations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Advertisements

Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Lecture 13 Decision structures
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Conditional Statements ENGR 1187 MATLAB 7. Conditional Statements in Real Life A Fluorescence Activated Cell Sorter (FACS) sorts cells based on predetermined.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
CSE123 Lecture 3 Files and File ManagementScripts.
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.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Lesson 1.4 Equations and Inequalities Goal: To learn how to solve equations and check solutions of equations and inequalities.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Chapter 2 Programming with PHP Part 2. handle_form.php Script 2.3 on pages 46 cript_02_03/form.html
“Operators” (i.e. “symbols”)
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
ITEC 2600 Introduction to Analytical Programming
Selections Java.
Introduction to Programming for Mechanical Engineers (ME 319)
Expressions and Control Flow in JavaScript
Introduction to MATLAB
JavaScript conditional
Lecture 3 MATLAB programming (1)
تصمیم‌گیری و کنترل روند، استفاده از حلقه‌ها و دستورات شرطی در متلب
Matlab review Matlab is a numerical analysis system
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Programming with PHP Part 2
Selection Statements Chapter 4 Attaway MATLAB 4E.
Logical Operations In Matlab.
CS2011 Introduction to Programming I Selections (II)
Conditional Statements
Conditional Logic Presentation Name Course Name
Matlab Basics.
Selection Statements Chapter 4 Attaway MATLAB 5E.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Announcements Exercise Sets 2 and 3 Due Thursday.
Presentation transcript:

Conditional Logic in MATLAB By Bruce Raine

How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional statement is true EX: >> if (a == b), disp('They equal each other'); end e.g. A ==B Relational operators (or comparators) > ==, =, ~= and other combos Logical operators> NOT (~), AND (&), logical OR (|), exclusive OR (xor)

Try these IF statements out (using ==, >) % set up conditional variables a = 5; b = a; % single-conditionals with many relationals/comparators disp('===================================') % uses == for equality if (a == b), disp('They equal each other'); end % uses == for equality if (a == 5), disp('a equals 5'); end %uses > if (a > 4), disp('a is greater than 4'); end

More IF statements using <= and LOGICAL AND (&) and LOGICAL NOT (TILDE,~) % uses <= if (b <= 7), disp('b is less than or equal to 7'); end b = 6; % uses TILDE for negation i.e. NOT EQUAL if (b ~= a), disp('b does not equal a'); end % a double condition using AND if a == 5 && b == 6, disp('they equal 5 and 6 respectively'); end

Even more IF statements (|, INCLUSIVE LOGICAL OR) % a double condition using INCLUSIVE OR b=7; if (a == 6 || b == 7), disp('one or other equals 6 or 7 respectively'); end % a double condition using numerical negation if -(a) == -5, disp('Negated a equals -5'); end

LOGICAL OR statements (using | and XOR) % a logical OR (either or both may be true) a=[1,0,0,1]; b=[0,1,1,1]; if (a | b), disp('Only one pair need be true'); end % exclusive OR (only one may be true) a1=[1 0 pi 0]; b1 =[ ]; if xor(a1,b1), disp('A match'); end

IF _ ELSE _ END structure if MATLAB Commands % do this if the conditional statement proves true else MATLAB Commands % do this if the conditional statement proves false end % place in a script file >> a= 3; b =5; if a <= b, v = 300; else v = 600; end

IF-ELSEIF-ELSE-END structure if MATLAB Commands % do this if the conditional statement proves true elseif MATLAB Commands % do this if the conditional statement proves false else MATLAB Commands % do this if the elseif proves false end

IF-ELSEIF-ELSE-END example % place in a script file >> a= 4; b =5; if a < b, v = 300 elseif a > b v = 600 else v = 500 end

SWITCH-CASE statement SWITCH switch-expression case value1 MATLAB commands case value2 MATLAB commands case value3 MATLAB commands … otherwise MATLAB commands end

SWITCH-CASE example units = input(‘Enter new units (a, b, c): ’,’s’); Error = 0 switch units case ‘a’ case ‘b’ case ‘c’ otherwise Error = 1; end