Day11_Part2_logicals_and_loading_files_Student_2020
Day11_Part2_logicals_and_loading_files_Student_2020
1
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Reference
} You should be following along in Chapter 4 of the
Attaway book (5th ed)
Ch 4: Logical
Expressions (4.1.1)
(related to if-else)
2
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Logicals
} a logical expression is an
expression that evaluates
to either true or false
} a logical variable is a
variable whose value is
either true or false
4
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Creating a logical variable
[Fill in here]
} the literals for logical true and false are the non-
keywords true and false
} however, these are rarely used (most people use 1 and 0)
>> x = true
x =
1
>> y = false
y =
0
5
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Relational operators
} more commonly, logical values arise from logical
expressions usually involving a relational operator
} relational operators produce logical values by
comparing two numbers
operator name
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
~= not equal to
6
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Relational operators
7
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1; [Fill in here]
>> y = 2;
>> x > y
ans =
0
>> x < y
ans =
8
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:8
x =
1 2 3 4 5 6 7 8
>> y = 8:-1:1
y =
8 7 6 5 4 3 2 1
>> x > y
ans =
0 0 0 0 1 1 1 1
9
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:8 [Fill in here]
x =
1 2 3 4 5 6 7 8
>> x > 5
ans =
0 0 0 0 0 1 1 1
ans =
10
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
11
12
Logicals
13
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Reference
} You should be following along in Chapter 4 of the
Attaway book (5th ed)
Ch 4: Logical
Expressions (4.1.1)
(related to if-else)
14
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Logical operators
} logical operators operate on logical values
} there are 5 logical operators and 1 function
Operator name
~ not
& elementwise and
&& short-circuit scalar and
| elementwise or
|| short-circuit scalar or
xor elementwise exclusive or
15
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
[Fill in here]
not ~
} ~ is Boolean negation (often called 'NOT')
} NOT true is equal to
} NOT false is equal to
expression result
NOT true false
NOT false true
16
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:5 [Fill in here]
x =
1 2 3 4 5
1 1 0 0 0
>> exist('x')
ans =
1
>> ~exist('x')
ans =
logical
17
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:5 [Fill in here]
x =
1 2 3 4 5
logical
0
>> ~any(x > 5)
ans =
logical
18
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
& elementwise and
} & is Boolean conjunction (often called 'AND') applied
elementwise
expression result
true AND true true
true AND false false
false AND true false
false AND false false
19
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = [1 1 0 0];
>> y = [1 0 1 0];
>> x & y
ans =
1 0 0 0
20
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:8;
x =
1 2 3 4 5 6 7 8
ans =
0 0 1 1 1 0 0 0
2<x<6
does not work
in most programming
languages
21
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
| elementwise or
} | is Boolean disjunction (often called 'OR') applied
elementwise
expression result
true OR true true
true OR false true
false OR true true
false OR false false
22
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = [1 1 0 0];
>> y = [1 0 1 0];
>> x | y
ans =
1 1 1 0
23
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> x = 1:8;
x =
1 2 3 4 5 6 7 8
ans =
1 1 0 0 0 1 1 1
24
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
25
26
Logicals
27
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Reference
} You should be following along in Chapter 4 of the
Attaway book (5th ed)
Ch 4: Logical
Expressions (4.1.1)
(related to if-else)
28
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Example
} if you roll two 6-sided dice, what are the odds that the
sum of the two dice is:
a) exactly 2?
b) exactly 7?
c) exactly 12?
d) less than 7?
e) greater than 7?
f) not 7?
29
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Solution via simulation
} idea:
} simulate many rolls and
count the number of
times a condition is met
(e.g., exactly 2, less than
7, etc.)
} divide by the number of
rolls to get an estimate of
the odds
} the more rolls you
simulate the more
accurate the estimated
odds
30 Images c/o The Noun Project
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> nRolls = 1000000; % 1 [Fill in here]
>> die1 = randi(6, 1, nRolls); % 2
>> die2 = randi(6, 1, nRolls); % 3
>> total = die1 + die2; % 4
1 million times!
31
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % exactly 2? [Fill in here]
>>
>> sum(total == 2)
ans =
27943
ans =
0.0279
32
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % exactly 7? [Fill in here]
>>
>> sum(total == 7)
ans =
166349
ans =
0.1663
33
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % exactly 12? [Fill in here]
>>
>> sum(total == 12)
ans =
27688
ans =
0.0277
34
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % less than 7? [Fill in here]
>>
>> sum(total < 7)
ans =
416305
ans =
0.4163
35
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % greater than 7? [Fill in here]
>>
>> sum(total > 7)
ans =
417344
ans =
0.4173
36
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
>> % not 7? [Fill in here]
>>
>> sum(total ~= 7)
ans =
833649
ans =
0.8336
37
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Simulation: Dice
for i=1:1:1000
nRolls = 5000*i;
die1 = randi(6,1,nRolls);
die2 = randi(6,1,nRolls);
total = die1+die2;
sum2(i) = sum(total==2)/nRolls;
sum12(i) = sum(total==12)/nRolls;
fprintf('Num of rolls: %d \t Num of 2: %d \t Num of 12: %d\n', nRolls, sum(total==2), sum(total==12));
end
figure(1);
subplot(2,1,1);
plot([1:1:1000],sum2,'-om',[1:1:1000],sum12,'-*c')
title('Convergence of Die Simulations');
legend('Result: 2', 'Result 12');
xlabel('Number of Rolls index (x5000)');
ylabel('Normalized Result');
38
Copyright James Andrew Smith; no permission to distribute or upload outside of YorkImages c/o The
University
Noun Project
Simulation: Dice
40
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Simulation: Dice
41
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Simulation: Dice
42
Copyright James Andrew Smith; no permission to distribute or upload outside of York University