Week_2_Part_2_Relational_and_Logical_Operators
Week_2_Part_2_Relational_and_Logical_Operators
Operators
Where do I find MATLAB? Here!
Before we jump into how these operators are defined or used operators, let us
consider our line of thinking when applying the pricing rules described in the
table above. The weight of the package is supplied to us, and we determine
which row of the Package Weight column the supplied weight fits. One could
start at the top or the bottom row, asking for example "Is 1.75 Kg less than or
equal to 1 Kg?". We know this is not true, so we move to the proceeding row,
asking; "Is 1.75 Kg greater than 1 Kg and less than or equal to 1.5 Kg?", and
so on. Once we find the pricing rule question that is true, we then refer to the
Shipping Price in that same row. The illustration below helps visualise this
graphically.
When values or numbers are being compared, for example "Is 1.75 Kg less
than or equal to 1 Kg?", we are making use of a relational operator, "less than
or equal to"/ . The outcome to a statement that includes a relational operator is
always logical, i.e. a true or false, and we can combine two or more logical
outcomes by means of a logical operator (and/or), for example "Is 1.75 Kg
greater than 1 Kg and less than or equal to 1.5 Kg?".
ans = logical
1
pi <= 3
ans = logical
0
Note: The outputs are of type logical.
Which elements of arr_2 is greater than 10? Assign the answer to the variable
logical_arr_2 for use later on.
Two potential customers approach us with an offer. The first potential customer
offers us ZAR 4.50 for a bread roll, the second offers us ZAR 8.00 for a bread
roll. As we are selling bread rolls with the intention of making a profit, we would
reject the offer made by the first potential customer and accept the offer from
the second one. This is because the selling price must be greater than our cost
price in order for us to generate profit from the transaction.
Let us consider the line of thinking that our brains might follow when choosing
to accept or reject the offers. Assume that the question we ask ourselves is, "Is
this offer profitable?". If this is true, we would consequently accept the offer,
and if it is false, we will reject the offer.
Let us test is each of the offers are greater than our cost price:
Is_Offer_1_Profitable = Offer_1_in_ZAR > Cost_price_in_ZAR
Is_Offer_1_Profitable = logical
0
Is_Offer_2_Profitable = Offer_2_in_ZAR > Cost_price_in_ZAR
Is_Offer_2_Profitable = logical
1
ans = logical
0
logical_1 | logical_2
ans = logical
1
~logical_1
ans = logical
0
logical_arr_1
logical_arr_1 = 1×10 logical array
1 1 0 0 1 0 1 0 1 1
logical_arr_2
logical_arr_2 = 1×10 logical array
1 1 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
© 2023 Introduction to Programming in MATLAB (2), Relational and Logical Operators 18
Logical Operators
Note: If you have cleared your workspace, you will need to run the section
where logical_arr_1 and logical_arr_2 are defined. The logical arrays
produced will be different each time you run the section, this is because they
are based on randomly generated integer arrays.
Now you try! Create a row vector, arr_5, with 5 elements between the values
of 1 and 10 using the randi function. Determine which elements are smaller
than 4 or greater than 6.
arr_5 = 1×5
3 8 8 4 6
The decisions we make every day are often complex and based on several
factors/conditions. In the relational operators section, we considered the most
simple form of decision making, i.e. there are two values/quantities that were
being compared. Here, we will be considering an instance where two outcomes
of relational operator conditions are combined to make a decision. The key here
is that two statements are being compared instead of two variables/values.
Let us consider a decision where we choose to light a candle or not for our
study area. Let us consider a case where we have access to an electric light
and a candle (with a working lighter) as the two sources of light in our study
area.
ans = logical
0
In the next part of this week's content, we will be learning about relational and
logical operators, covering the following sub-topics:
Copyright 2022 The MathWorks, Inc. & Opti-Num Solutions (Pty) Ltd.
• Week_2_Part_2_Relational_and_Logical_Operators.mlx