## Description

(1) (4 points) Working with numbers in matrices.

(a) Create a 4 × 4 matrix of numbers sampled from a standard Normal distribution. It is highly

unlikely, but if your random matrix only has positive or negative values, generate a new one

with both positive and negative values. The probability of this happening is < 0.001, so if it
does happen to you make sure you’re generating normal values. Display the values.
(b) Extract the values that are positive and put them in a vector. Display the values. Do not
use a loop.
(c) Use the find function to extract the row and column indices of the positive values. You may
need to use Google or the MATLAB help to figure this out. Display these values.
(d) Create a 4 × 4 matrix that has values of 0 where your original matrix was non-positive and
1 where it was positive. Display the values.
(2) (3 points) Calculating Euclidean distance. The Euclidean distance between two vectors, x =
[x1, x2, . . . , xn] and y = [y1, y2, . . . , yn] is defined as
d(x, y) = p
(x1 − y1)
2 + (x2 − y2)
2 + . . . + (xn − yn)
2.
So the distance between x = [1, 2] and y = [3, 5] is p
(1 − 3)2 + (2 − 5)2 =
√
13 (note that is in
2D). We will use Euclidean distance many times in this class to see how close a pixel magnitude in
a image is to some value. For your matrix in problem 1a, create a new matrix where each entry is
the Euclidean distance from the entry in the first row/first column. Do this without using a loop.
(3) (7 points) Reading in a ppm file and differentiating between image display options in MATLAB.
(a) (3 points) Use Google or the MATLAB help to read in the img_001.ppm image that was
distributed with this assignment. You cannot use a downloaded MATLAB function to do
1
this, but must use a built in MATLAB function. After reading in the image, display the
dimension of the image and describe what each dimension represents. What is the range of
values in the image?
(b) (2 points) Use Google or the MATLAB help to learn about the differences between imshow,
image and imagesc. For each one describe how it differs from the other 2, including when it
is optimal to use that specific function to display an image. Make 3 separate figures, where
each one displays img_001.ppm using each of these commands. Note: Do not use a multipanel
plot for this exercise.
(c) (2 points) Using a multipanel plot (1 × 3) and imagesc() to display each of the 605 × 700
images, separately in grayscale. Use a proper plot title to describe what the image is showing.
I’m looking for more than 1, 2 and 3. I’d like the meaning of the image. You may need to
google how to display in grayscale. How is imagesc working differently here than it did in
the previous part (aside from the fact that I had you change it to grayscale)? Tip, the
axis(’square’) command may make the plot look a little nicer.
(4) (10 points) Filtering an image. We will learn numerous ways to smooth an image and to practice
manipulating matrices, you will mean filter the image from the previous problem. Specifically, use
the red channel portion of the image (img_001.ppm). Importantly please change the values to
double precision array using the double(). If you don’t your new image will only have integer
values and they should be real number values. The following image describes how the mean filter
works. The left hand side is the starting image and right hand side is the filtered image. For now,
we’ll ignore the edges and simply put 0s for the edges and fill in the rest. For each pixel (entry in
the matrix) construct a 3×3 neighborhood around the pixel. The center of this neighborhood is the
target in the new matrix (where your value will go) and the value is the mean of the 9 values in the
neighborhood. The image illustrates the first two pixels. You may not use any built in MATLAB
filtering functions or convolution functions for this problem, but must code it from scratch using
a loop (unless you can think of a way to avoid the loop). Filter the image and print out the first
5 entries in the first 5 rows (if the image is called img_filt, then use img_filt(1,1:5)). Also
make a 1 × 2 panel plot where the left panel is the original image and the right panel is the filtered
image.
5 6 2 7 2 9
1 1 2 7 4 8
3 4 2 7 8 9
4 2 5 7 8 8
1 2 1 4 3 5
2 3 8 9 7 6
0 0 0 0 0 0
0 2.9 4.2 7 4 0
0 4 2 7 8 0
0 2 5 7 8 0
0 2 1 4 3 0
0 0 0 0 0 0
Original Image Filtered Image
mean([5,6,2,1,1,2,3,4,2]) mean([6,2,7,1,2,7,4,2,7])])
2
(5) (4 points) In this problem you will make a 3 dimensional plot using the imagesc command.
The result should be a word commonly used to greet people. The first two dimensions are x and y
and describe the location of a pixel (2D unit of an image) while the third dimension can be thought
of as a pixel magnitude. Step 1 is to create the grid of x and y coordinates. Step 2 will create the
the pixel magnitude.
(a) Create a and b as vectors that go from -3 to 3 in increments of 0.1.
(b) You’ll be evaluating a function over the values of a and b in all pairwise combinations. Use
the meshgrid command, [A, B]=meshgrid(a,b);. Print the size of A to the screen and using
imagesc create a 1 × 2 panel plot that shows A on the left and B on the right. Describe what
meshgrid did in one or two sentences. Don’t just say it created a mesh grid 🙂 Explain what
it is doing. It might help to look at the output from [testA, testB]=meshgrid(1:2, 1:3)
as well.
(c) Create Z according to Z = exp−A2−B2/2
cos(4A) + exp−3((A+0.5)2+B2/2). Change values in
Z that are greater than 0.001 to 0.001 and less than -0.001 to -0.001. Plot the result using
imagesc. If you don’t see a commonly used word to greet people, you’ve made a mistake.
(d) What are the 3 dimensions in this image? Specifically, describe them and give their ranges.
3