[go: up one dir, main page]

0% found this document useful (0 votes)
5 views41 pages

Day11_Part2_logicals_and_loading_files_Student_2020

The document covers logical expressions and variables in MATLAB, explaining how they evaluate to true or false using 1 and 0. It details relational and logical operators, providing examples of their usage in MATLAB code. Additionally, it includes a simulation example of rolling dice to estimate probabilities based on logical conditions.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views41 pages

Day11_Part2_logicals_and_loading_files_Student_2020

The document covers logical expressions and variables in MATLAB, explaining how they evaluate to true or false using 1 and 0. It details relational and logical operators, providing examples of their usage in MATLAB code. Additionally, it includes a simulation example of rolling dice to estimate probabilities based on logical conditions.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Logicals

EECS 1011: Class Session 11

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

} logical variables are


usually called Boolean
variables in computer
science
3
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
Logicals in MATLAB
} MATLAB uses the numbers 1 and 0 to
represent true and false
} that is, every logical expression will
evaluate to either exactly 1 (true) or 0
(false), and the value of every logical
variable will be either exactly 1 or 0
} however, MATLAB will convert any
non-zero, non-NaN numeric value to
logical 1
} only values equal to exactly 0 are
converted to logical 0

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

} the relational operators will operate in an element by


element fashion for arrays
} you can also compare a scalar versus an array

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 =

1×8 logical array

0 0 0 0 0 1 1 1

>> sum(x > 5)

ans =

10
Copyright James Andrew Smith; no permission to distribute or upload outside of York University
11
12
Logicals

EECS 1011: Class Session 11

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

>> ~(x > 2) % parentheses needed (precedence)


ans =

1×5 logical array

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

>> any(x > 5)


ans =

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

>> x > 2 & x < 6

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

>> x <= 2 | x >= 6

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

EECS 1011: Class Session 11

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. this is the number of rolls of two dice that we will simulate


2. a vector of nRolls random integer values between 1 and 6
3. a vector of nRolls random integer values between 1 and 6
4. a vector of nRolls integer values representing the sum of two
randomly thrown dice

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

>> sum(total == 2) / nRolls

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

>> sum(total == 7) / nRolls

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

>> sum(total == 12) / nRolls

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

>> sum(total < 7) / nRolls

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

>> sum(total > 7) / nRolls

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

>> sum(total ~= 7) / nRolls

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');

subplot(2,1,2); % second plot


plot([1:1:1000],5000*[1:1:1000]);
title('Number of die rolls per iteration');
ylabel('Numbr of rolls');
xlabel('Loop iteration number');

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

You might also like