Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new...
Transcript of Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new...
Lab 1 Recap: some new commands
randi(maxVal, numRows, numCols)
Produces an array with the specified number of rows and columns. Each entry in the array is a random integer between 1 and maxVal.
Example:
>>myMatrix = randi(6, 2, 3)
myMatrix =
4 6 1
2 3 4
Lab 1 Recap: some new commands
someArray == someValueProduces an array with the same size as someArray. The new array has a 1 in positions where someArray has the value someValue, and 0 otherwise.
Example:
>>myMatrix == 4
ans =
1 0 0
0 0 1
from previous slide
Lab 1 Recap: some new commands
find(someVector)
Returns the positions of the nonzero entries in someVector.
Example:
>>find([0 0 3 0 2 0 0 1])
ans =
3 5 8
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
>>dieTosses = randi(4, 1, 12)
‣ 1 x 12 array dieTosses is created
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ 1 x 12 array dieTosses is created
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ array is filled with random integers between 1 and 4
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ array is filled with random integers between 1 and 4
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ array is filled with random integers between 1 and 4
‣ these are our simulated die tosses
Lab 1 Recap: simulate die tosses
>>
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
Lab 1 Recap: simulate die tosses
>>
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
Lab 1 Recap: simulate die tosses
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ 1 x 12 array myFours is created
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ 1 x 12 array myFours is created
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ myFours has a 1 in every position that dieTosses has a 4, and 0’s elsewhere
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ myFours has a 1 in every position that dieTosses has a 4, and 0’s elsewhere
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ Question: what will this command do?
Lab 1 Recap: flag the 4’s using ==
>>myFours = (dieTosses == 4)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ Question: what will this command do?
>>numFours = sum(myFours)
Lab 1 Recap: flag the 4’s using ==
>>
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
Lab 1 Recap: flag the 4’s using ==
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
‣ find(myFours) returns the positions of the nonzero entries in myFours
Lab 1 Recap: find times of the fours
>>fourTimes = find(myFours)
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
‣ Question: what will this command do?
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
‣ Question: what will this command do?>>whatIsThis = fourTimes(5)
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
‣ Question: what will this command do?>>whatIsThis = fourTimes(5)
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
‣ Question: what will this command do?>>whatIsThis = fourTimes(5)
Lab 1 Recap: find times of the fours
4 4 1 4 3 1 2 3 4 4 1 4
dieTosses (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 1 0 1 0 0 0 0 1 1 0 1
myFours (1 x 12 array)
1 2 3 4 5 6 7 8 9 10 11 12
1 2 4 9 10 12
fourTimes (1 x 6 array)
1 2 3 4 5 6
‣ Question: what will this command do?>>whatIsThis = fourTimes(5)
Lab 1 Recap: find times of the fours