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...
![Page 1: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/1.jpg)
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
![Page 2: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/2.jpg)
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
![Page 3: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/3.jpg)
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
![Page 4: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/4.jpg)
Lab 1 Recap: simulate die tosses
>>dieTosses = randi(4, 1, 12)
![Page 5: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/5.jpg)
>>dieTosses = randi(4, 1, 12)
‣ 1 x 12 array dieTosses is created
Lab 1 Recap: simulate die tosses
![Page 6: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/6.jpg)
>>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
![Page 7: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/7.jpg)
>>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
![Page 8: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/8.jpg)
>>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
![Page 9: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/9.jpg)
>>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
![Page 10: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/10.jpg)
>>
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
![Page 11: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/11.jpg)
>>
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
![Page 12: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/12.jpg)
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
![Page 13: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/13.jpg)
>>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 ==
![Page 14: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/14.jpg)
>>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 ==
![Page 15: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/15.jpg)
>>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 ==
![Page 16: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/16.jpg)
>>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 ==
![Page 17: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/17.jpg)
>>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 ==
![Page 18: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/18.jpg)
>>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 ==
![Page 19: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/19.jpg)
>>
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 ==
![Page 20: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/20.jpg)
>>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
![Page 21: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/21.jpg)
>>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
![Page 22: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/22.jpg)
>>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
![Page 23: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/23.jpg)
>>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
![Page 24: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/24.jpg)
>>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
![Page 25: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/25.jpg)
>>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
![Page 26: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/26.jpg)
>>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
![Page 27: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/27.jpg)
>>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
![Page 28: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/28.jpg)
>>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
![Page 29: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/29.jpg)
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
![Page 30: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/30.jpg)
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
![Page 31: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/31.jpg)
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
![Page 32: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/32.jpg)
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
![Page 33: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/33.jpg)
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
![Page 34: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/34.jpg)
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
![Page 35: Lab 1 Recap: some new commands - Hampden-Sydney College · 2020. 9. 29. · Lab 1 Recap: some new commands someArray == someValue Produces an array with the same size as someArray.](https://reader035.fdocuments.in/reader035/viewer/2022071401/60eaf52c91d99c076f305524/html5/thumbnails/35.jpg)
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