lab 1 recap: some new commands - hampden-sydney college · 2020. 9. 29. · lab 1 recap: some new...

35
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

Upload: others

Post on 21-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

>>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

>>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

>>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

>>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

>>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

>>

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

>>

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

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

>>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

>>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

>>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

>>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

>>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

>>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

>>

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

>>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

>>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

>>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

>>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

>>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

>>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

>>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

>>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

>>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

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

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

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

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

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

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

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