Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Can you complete the assignment as mentioned in the pdf file

Can you complete the assignment as mentioned in the pdf file

Mechanical Engineering

Can you complete the assignment as mentioned in the pdf file. I will be giving you one gen_array.m file which is a function you will be using in a separate .m file. Don't forget to install the Statistics and Machine Learning Toolbox in MatLab for the file to work. Please refer the pdf file and complete the assignment in a separate .m file.

1) Use the provided function named gen_array.m to create an array called guess_array that

2 stores all possible combinations of d-letter long codes from the n letters in use. Do not

3 alter the gen_array.m function!! Do not paste this function in your m-file – it should

4 reside in a separate file, as provided to you. The inputs to gen_array are n and d , and the

5 output is an array with all possible codes given the values of n and d. Call this function

6 as:

7 guess_array=gen_array(n, d)

8

9 For example, if n=6 and d=4, then there are 360 possible codes (6x5x4x3). Note that

10 each code is stored as a row within guess_array; thus, for the case n=6 and d=4,

11 guess_array will be a 360x4 character array. Also, note that if n<10, gen_array will

12 randomly pick n letters from the alphabet to work with.

13 2) To simulate the Code Maker, randomly select a single row from guess_array, and assign

14 it to the variable code. This is the code your function must guess correctly. (Hint: To

15 accomplish random selection, look up the Matlab help on datasample.)

16 3) Now randomly select an initial guess from guess_array. Call this variable guess.

17 4) Initialize a logical vector called TF. It should be the same size as guess_array, and should

18 be initialized as all true. (See Matlab help on “true”.) This will help when eliminating

19 guesses later.

20 5) Initialize rounds with a value of 1.

21 6) Now it’s time to play the game. Use a while loop that runs while guess and code are NOT

22 equal and while the number of rounds is less than or equal to 8. For each iteration of the

23 while loop:

24 a. Create a subfunction called check_matches with two inputs (array1, array2) and

25 one output (nmatches). The inputs are just two character arrays, and the output is

26 the number of matches in the characters between the two arrays. For example, if

27 you call check_matches(1#$5, 5#$2), then the output should be nmatches=2. Note

28 that ‘5’, while present in both arrays, is in different locations and therefore not

29 considered a match.

30 b. Given the output of check_matches, you can eliminate many possibilities from

31 your guess_array. You should eliminate any number in the guess_array that does

32 not have the same number of matches with guess as the matches returned

33 from check_matches. For example, if you guess 1#$5 and check_matches returns

34 1, you will eliminate any array that doesn’t have exactly one match with 1#$5

35 (examples: $32# - no matches, 2#@5 - 2 matches; 1#$3 - 3 matches, etc.) Use a

36 for loop to compare each value in guess_array to guess and change the

37 corresponding element in TF to false if the number of matches is not correct.

38 c. Use your TF vector to eliminate guesses from guess_array. guess_array will

39 shrink in size with each round.

40 d. Now, select a new guess from the updated guess_array. Increment the value of

41 rounds by 1, and update TF so that it is the same size as guess_array and has all

42 true values once again. Repeat the above steps to continue the game, which will

43 end once guess is equal to answer or if rounds exceeds 8.

44

45 YOUR SCRIPT

46 Create a script that will examine the changes that occur with different combinations of n and d.

3

1

2 1) Clear all variables and shuffle the seed.

3 2) We will test three combinations of inputs: n = 6, d = 4; n = 8, d = 5; and n = 9, d = 6. So,

4 create a 3x2 matrix of inputs, with n in the first column and d in the second column.

5 3) Use two nested for loops to test each combination N times. Store the output of your

6 function in a 3xN matrix called rounds_all, with each row corresponding to each of the

7 three input combinations, and each column being one realization of playing the game.

8 Remember the output of your function is the number of rounds played to guess the

9 answer. When your code is done, set N=20. When testing though, make it a smaller value

10 otherwise it will take too long.

11 4) Create three separate histograms for the values of rounds_all for each combination of

12 inputs. Use subplot to put them on one figure, and use titles and labels to make it clear

13 which is which.

14 5) Calculate the win percentage for each combination of n and d and display this in the

15 command window. Reminder: a WIN is when the Code Breaker guesses the code in 8 or

16 fewer rounds. Anything greater is a LOSS.

17

18

19 Notes:

20 - Your script may take a while to run, as it requires a lot of looping.

21 - Make sure you label your graphs correctly.

22 - Since your code may take a while to run, use your debugger to run separate sections of

23 your code.

24 - To test your code, use something like n=3 and d=2 and use the debugger to walk through

25 the code line by line. There are only 6 combinations in this case, so it’s easy to see if your

26 logic is correct and if the loops are working.

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE