CS 1132 lab exercise 4

Do all three problems first using Matlab, testing and debugging using your own analysis and test cases. Then submit your solutions for Problems 1 & 3 on Matlab Grader by Wednesday, September 29, at 2:30pm (before next week’s lab). Note that Matlab Grader is not able to definitively establish the correctness of your solution in Problem 3 due to the variability in how pseudorandom values are used in different solutions—if your solution fails a test case then most definitely your solution is incorrect (or not appropriate); if your solution passes all tests, there still is a chance that the solution is incorrect. So be sure to do your own testing and talk with a human course staff member when in doubt.

Smoothing a matrix

Consider a 2-D array A that has at least 4 rows and at least 4 columns. We define (i,j) as an interior point if A(i,j) is not located in the first row, first column, last row, or last column of A. Define the neighborhood of an interior point (i,j) as the 3-by-3 sub-array of A with the centermost element being A(i,j). For example, if A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16], the neighborhood of the interior point (2,2) is the subarray [1 2 3; 5 6 7; 9 10 11]. Implement the following function as specified. You are strongly encouraged to use vectorized code whenever appropriate.

function M = smoothMatrix(A)
% "Smooth out" the entries of the 2-D array A to obtain the 2-D array M.
% Input:
%  A: an m x n 2-D array where m and n are each >= 4.
% Output:
%  M: an m x n 2-D array whose elements are obtained as follows:
%    if (i,j) is an interior point, M(i,j) is computed as the average of 
%    the 9 elements in the neighborhood of A(i,j). Otherwise, M(i,j) = A(i,j).

Determinant of a 3×3 matrix

Write a function myDeterminant(x), where x is a 3×3 matrix. Use the following formula:

$$ \det\left(\left(\begin{array}{ccc} a & b & c\\ d & e & f\\ g & h & i\end{array}\right)\right)=a\det\left(\left(\begin{array}{cc} e & f\\ h & i\end{array}\right)\right)-b\det\left(\left(\begin{array}{cc} d & f\\ g & i\end{array}\right)\right)+c\det\left(\left(\begin{array}{cc} d & e\\ g & h\end{array}\right)\right) $$

Use the built-in function det() to find the determinant of each 2×2 matrix. For example, det(m) returns the determinant of 2×2 matrix m. Then use the formula given above to calculate the determinant of a 3×3 matrix. This question is all about practicing how to access individual components or submatrices in a matrix. Recall that you can construct a matrix by putting two row vectors one below the other or putting two column vectors side by side.

Rolling fair dice

Download and read function rollDie() from the course website. rollDie(n) simulates the rolling of one fair six-sided die n times. The function draws a histogram of the simulation result, an example of which is shown below:

Histogram from rolling one die

First call the function with different arguments, for example, try rollDie(10), rollDie(100), rollDie(10000). Do the histograms look reasonable? Next read the code and observe the following:

Raise your hand if you have any questions on the given code.

Task: Write a function rollDice(n,d) to simulate the rolling of d six-sided dice n times. We define the outcome of rolling d dice once to be the sum of the faces that show up. The function returns a vector count where count(c) is the number of times that outcome c has occurred. The length of vector count is 6×d. In this exercise use functions rand() and floor() appropriately to generate random integer values; do not use function randi(). Your function shoudl also draw a histogram of the result using function bar(). Below is an example histogram for small n. What shape do you expect to see for large n?

Histogram from rolling two dice

Work on Assignment 2!

Be sure to log off the lab computer before leaving the lab.