Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 3

GENERAL INSTRUCTIONS: Store your work as a .m file named hw3_<YOUR STUDENT    ID>.m (replace <YOUR STUDENT ID> with your own 9-digit student ID number). Upload this   file to CCLE as your homework submission. Your entire .m file should run without errors. The   script should begin with the command clear and, when it completes, all variables listed at the end of this document should be in the workspace.

Each problem part indicates that a variable is to be stored in memory. When your script is         executed in its entirety, the resulting variables should be the solutions to the corresponding       problem parts. Once you have found the answer to a problem, do not modify that variable in     subsequent lines of code; if needed, create a new version of the variable with a different name.

NOTE: Using semicolons to put multiple commands on one line of text in MATLAB (e.g.,         x=1;x=x+1;) does not count as using a “single line of code” . Any problem that doesnt ask for a single line of code may be solved with any number of lines of code.

1.   Modify the craps simulation study from W3P1 slide 17 to estimate the probability of winning a game of hazard. You can find the rules to hazard here:

https://wizardofodds.com/games/hazard/

Use French rules (see the webpage) to determine the main. There are many differences      between hazard and craps, so be sure to account for all of them. Make MALAB play hazard 100,000 times. Before the for loop, set a random number seed using rng that yields an     estimated probability of winning between .490 and .492 (the true probability is about             0.490825). This may require trying a few different seeds. Store your estimated probability as pWinHazard (4 points)

HINT: Use the tools you have; you will need more conditionals (and more things in those conditionals) to follow all the extra rules, and you’ll need a while loop to determine the  main at the start of each game of hazard.

2.   Generalize your solution to question 1 in Homework 2 (the plus sign problem) to create   matrices of similar structure but of arbitrary size. In particular, write code that begins with initializing of the size of the matrix of squares:

plusSize=8;

When plusSize (and nothing else) is changed, flexible code will produce a plusSize by plusSize matrix of 0s with a plus-sign shape of 1’s in the middle.

Starting with the above command, write code to create a plusSize by plusSize matrix of 0s with a plus symbol of 1s inside. To demonstrate your code, create a 31 by 31 such     matrix by setting plusSize =31 and store the result as plusSign31 (3 points). If        plusSize is odd, the vertical region of 1s should be 3 elements wide and the horizontal      section should be 3 elements tall; if, instead, plusSize is even, the vertical region of 1s      should be 2 elements wide and the horizontal section should be 2 elements tall.

For instance, setting plusSize =8 should yield the solution to Question 2 on Homework 2, and setting plusSize =11 should yield the following matrix:

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1 1

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

0 0 0 0 1 1 1 0 0 0 0

a.   EXTRA CREDIT: You can solve this problem three ways: use loops and conditionals, concatenate matrices together without loops, or using vectorized operations, working only with plusSize by plusSize matrices. For extra credit, solve the problem in multiple ways. Specifically, the three ways are:

1.  Start with a plusSize by plusSize matrix of NaNs and use loops (one or two) to replace each element with a 0 or 1 individually, analogously to the                 multiplication table problem from W2P1 lecture.

2.   Use concatenation, creating matrices of 0s and 1s based on plusSize and    combining them together with square brackets, spaces/commas, semicolons, and/or the cat family of functions

3.  Solve the problem using vectorized operations with only plusSize by plusSize matrices and scalars.

In terms of specific grading rules: If you solve the problem in only one way, you get   the points for the regular non-extra-credit problem (3 points). Each addition           solution gets you 1 point of extra credit for a maximum of 2 points of extra credit  (5 points total). If you solve the problem in multiple ways, name your variables       different things: plusSign31loops when using loops, plusSign31concat when using concatenation, and/or plusSign31vector when using vectorization. Again, if you only solve the problem one of these ways, simply name your result               plusSign31.

3.   Load the patients.mat data file from lecture using the following code:

load patients.mat;

a.   Recode the variable LastName to be in Pig Latin. This requires doing the following:

•    If the first letter of the name is not a vowel (vowels are A E I O U), convert the first letter to lower-case, move all letters before the first vowel to the end of the word        (append them to the end, don’t replace the last letter), and convert the first letter of   the resulting word to upper-case. If the first letter is a vowel, skip this step.

•    Then, add the letters 'ay' to the end of the word.

For example, 'Smith ' should become 'Ithsmay ', and 'Evans' becomes 'Evansay '

Name the resulting variable AstNamelay, which should, like LastName, be a cell array of character row vectors (3 points). These operations should all be done                         programmatically; do not enter the resulting words into MATLAB directly. Providing a


different, similarly formatted cell array of character vectors of names (all single words,  first letter capitalized) should result in those words being converted to Pig Latin. We will test this.

HINT: Remember that character arrays in MATLAB are stored internally as arrays of  numbers. This means that you can use indexing to rearrange the letters in a character array, e.g. x=[x(end),x(1:(end-1))]. This also makes it easier to convert upper- case letters to lower-case letters and vice versa. Specifically, if you have a single        character, say

x= 's'

thats in lower case, you can subtract 32 and convert to character:

char(x-32)

and the result will be the same letter but in upper-case. Likewise, you can add 32 to  convert upper-case letters to lower-case. If you can find built-in MATLAB functions to help you with this problem, you are welcome to use them, but it is not required or      expected to do so.

b.   Create a table called patients which stores all variables in the patients.mat data file. The names of the variables should be in alphabetical order as they appear in the     workspace, starting with Age in the first column and ending with Weight in the last        column. Also include AstNamelay in this table in its alphabetical position: after Age,  before Diastolic (1 points)

c.   Using a single line of code, create a new table called youngPatients which stores   only the rows of patients corresponding to patients age 30 and under. Using a            separate line of code, create a new table called oldPatients which stores only the  rows of patients corresponding to patients age 31 and over. Your patients data set should stay as-is, but you can index it (I recommend logical indexing, but it’s not             required) to create the new tables (.5 points each).

d.   Create two variables called meanYoungMen and meanYoungWomen, storing the mean  systolic blood pressure for men age 30 and under in meanYoungMen and the mean       systolic blood pressure for women age 30 and under in meanYoungWomen. Do not use the split-apply-combine workflow from lecture for this problem; instead, use logical         indexing, a loop, or some other method (.25 points each). Then, do the same for the      older patients, creating two variables called meanOldMen and meanOldWomen, storing the mean systolic blood pressure for men age 31 and over in meanOldMen and the       mean systolic blood pressure for women age 31 and over in meanOldWomen. For these two, use the split-apply-combine workflow from lecture (.25 points each). To ensure you have the right numbers, I recommend checking your answer for each by solving both     problems in both ways, and/or using hand calculations to make sure you’re matching up the numbers properly, but you don’t need to include this code in your submission.

Upon completion, your workspace should contain the following variables (bold and italic = extra credit; see question 2a instructions for details):


pWinHazard      plusSign31

plusSign31loops      plusSign31concat     plusSign31vector

AstNamelay      patients        youngPatients        oldPatients

meanYoungMen    meanYoungWomen       meanOldMen      meanOldWomen