Logic
Last updated on 2025-04-28 | Edit this page
Estimated time: 12 minutes
Overview
Questions
- How can I make my code do different things when variables change?
- How can I compare values in an array to other values?
- How can I select subsets of my data depending on a comparison?
Objectives
- Learn to use relational operators to compare values
- Understand if and else statements to create conditional code
- Learn some new tools and functions relating to logic
Relational Operators
Relational operators are used to compare the values in 2 arrays and returns a logical array.
Here are the most common logical operators in MATLAB:
MATLAB
data == 50 % Are any data points equal to 50?
data ~= 50 % Are any data points not equal to 50?
data > 50 % Are any data points greater than 50?
data < 50 % Are any data points less than 50?
data >= 50 % Are any data points greater than or equal to 50?
data <= 50 % Are any data points less than or equal to 50?
Here is an example of comparing two arrays:
OUTPUT
ans =
1×4 logical array
1 0 1 0
MATLAB Logic
MATLAB represents logical true and false statements with 1 and 0, where 1 is true and 0 is false
The returned logical array tells us that the first values were equal, the second not equal, etc.
If Statements
If statements allow us to execute different lines of code depending on a logical value.
OUTPUT
c is positive
In this example, c > 0
is called the
condition, disp('c is positive')
is the
code which will be executed if the condition is true, and every if
statement in MATLAB should be terminated with an end
We can use some inbuilt functions such as any
or
all
to test a condition across an entire array.
For example, we have a data set representing student marks called data, where each value is a mark out of 100, each row a student and each column a subject:
We could test if any there any marks below or equal to 10 with the following code
Challenge
Using the student_marks
data above, display a sentence
if any student achieves a first (70 or more) in the 3rd subject
Logical Indexing
In the working with variables chapter, we looked at how we can index a variable with row and column numbers.
OUTPUT
8
Here we will look at indexing using logic arrays, which going back are the result of relational operators.
OUTPUT
ans =
4
5
6
7
8
First we create a logical array called big_data
, which
is the result of us looking for numbers in our data greater than or
equal to 4.
We can then use this to index data, like slicing by index number, the result being only the part of data that is bigger than 4.
These two lines can be combined as well, avoiding the need to save the logical array as it’s own variable
OUTPUT
ans =
4
5
6
7
8
logical not
A useful tool when handling logical arrays is the logical not,
represented with the ~
symbol.
This operation inverts a logical array, which can be used to find values that don’t meet a condition.
OUTPUT
0 0 1 1
0 1 1 1
1 1 0 0
1 0 0 0
isnan
The final thing we will look at in this episode is the
isnan
function. This function returns a logical array
containing true (1) where there is a NaN in the array. This could be
used to replace the NaN values with a default value like 0, or combined
with the logical not to only select non-nan data:
OUTPUT
data =
2
1
4
5
8
7
Key Points
- The result of a relational operator is a logical array
- if, elseif and else can be used to create powerful conditions
- Logical arrays can be used to index variables