MATLAB Basics 8conditionalsif statements
Anthony Rossiter
University of Sheffield
1
Slides by Anthony Rossiter
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html
Introduction
1. We have established that using script files for sequences of operations is useful.
2. Sometimes, the specific computation we want to do depends upon the outcome of a previous computation – such decisions are denoted as conditional operations.
3. This resource gives an introduction to basic conditional operations that can be coded in MATLAB.
Slides by Anthony Rossiter
2
The coding is largely intuitive and follows reasoning common to humans and thus is easy and transparent to code.
What is logic in programming?
Many circumstances need decisions.
The action a computer code takes may depend on a decision, for instance:
1. How big should the axis be?
2. Should I use algorithm 1 or algorithm 2?
3. Do I have enough information to solve this problem?
4. Has the user supplied a proper value or made a typo?
5. Etc.
These slides look at how to incorporate decision making into code.
Common conditional operations
• Equality comparisons (numbers).
• Greater than and less than comparisons.
• Yes/No (logical variables).
• Other logical operations (AND, OR, etc).
• Text comparisons.
• Membership tests.
• For a more complete list use: >> help ops
Slides by Anthony Rossiter
4
HERE the focus is on the ‘if’ statement.
Common numerical comparisons
• The action depends upon whether a given variable is bigger or smaller than a set value: e.g. if too cold, turn on the heating.
• The action relies on two values being equal: e.g. if the two persons share a birthday, celebrate together.
• The action depends upon a multiple of numeric tests: e.g. if the date is in the summer and I have more than £30 spare cash, then I can buy that new swimsuit.
Slides by Anthony Rossiter
5
MATLAB commands for comparison
• If a>b; CODE; end; [Alternate: if gt(a,b) ….. ]
Action the CODE if the numerical value of a is greater than the numerical value of b. Otherwise, do nothing.
• If a<=b; CODE; end; [Alternate: if le(a,b) ….. ]
Action the CODE if the numerical value of a is less than or equal to the numerical value of b. Otherwise, do nothing.
Slides by Anthony Rossiter
6
MATLAB commands for comparison
If a==b strict equality [Alternate: if eq(a,b)]
If a>=b greater than or equal to [Alternate: ge(a,b)]
If a<b less than [Alternate: if lt(a,b)]
Slides by Anthony Rossiter
7
Combining commands
MATLAB supports the logical operations of
AND, OR, XOR, NOT
AND: BOTH CONDITIONS MUST HOLD
If (a==b)&(c>d); CODE; end and(a==b,c>d)
OR: Either condition may hold
If (a==b)|(c>d); CODE; end or(a==b,c>d)
XOR: one but not both conditions must hold
NOT: condition must be false
Slides by Anthony Rossiter
8
matlab_basics8a.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The if statement
a=1;b=1;
if a<b;disp('Yes a is less than b');end
if a>b;disp('Yes a is greater than b');end
if a==b;disp('a and b are the same');end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Slides by Anthony Rossiter
10
matlab_basics8b.m
a=-1
if a >=2; answer='high';
elseif a>=1; answer='middle';
elseif a>=0; answer = 'low'
else; answer='none'
end
disp('***************');
disp([sprintf('a is %g',a),' ',answer])
disp('***************');
Slides by Anthony Rossiter
11
SequentionalCONDITIONALS
using elseif
matlab_basics8c.ma=3.3;b=4.2;
if and(ge(a,b),le(a,1));disp('a is greater than or equal to b and less than or equal 1');
end
if and(lt(a,b),gt(a,1));disp('a is less than b and greater than 1');
end
if or((a<b),(b==2));disp('Either a is less than b or b=2');
end
Slides by Anthony Rossiter
12
Logical OPERATIONS
matlab_basics8a.ma=1.3;b=4.2;
if a>2;
if and(ge(a,b),le(a,3));
disp('a is greater than or equal to b and less than or equal 3');
end
if and(lt(a,b),gt(a,2.1));
disp('a is less than b and greater than 2.1');
end
elseif a<0;
disp('a is negative');
else
if b>5;
disp('a is between 0 and 2 and b is greater than 5');
else
disp('a is between 0 and 2 and b is less than 5');
end
endSlides by Anthony Rossiter
13
NESTED CONDITIONALS
Conclusions
1. Demonstrated the use of ‘if – elseif -- end’ statements in MATLAB.
2. Primarily focussed here on numerical comparisons such as equality, greater or lesser.
3. It is possible to compare strings and matrices, but some care is needed then.
The core principle is that CODE is implemented if and only if the conditional statement is satisfied.
Slides by Anthony Rossiter
14
© 2018 University of Sheffield
This work is licensed under the Creative Commons Attribution 2.0 UK: England & Wales Licence. To view a copy of this licence, visit http://creativecommons.org/licenses/by/2.0/uk/ or send a letter to: Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
It should be noted that some of the materials contained within this resource are subject to third party rights and any copyright notices must remain with these materials in the event of reuse or repurposing.
If there are third party images within the resource please do not remove or alter any of the copyright notices or website details shown below the image.
(Please list details of the third party rights contained within this work.
If you include your institutions logo on the cover please include reference to the fact that it is a trade mark and all copyright in that image is reserved.)
Anthony RossiterDepartment of Automatic Control and
Systems EngineeringUniversity of Sheffieldwww.shef.ac.uk/acse
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html