You are on page 1of 6

CS30 Spring 2014

Lab 9
Use the command diary to record your answers and submit them. Submit code for the functions and
scripts you write. Submit any gures as indicated.
1. (40 points) SVD based image compression. Given a matrix A, the singular value decomposition (SVD)
of the matrix is a decomposition
T
A = U V
where U and V are orthonormal matrices and is the diagonal matrix of nonnegative sinuglar values.
is sorted with the larged singular value at max = (1, 1) and the other singular values in decreasing
order down the diagonal. Matlab computes the SVD using the function svd. This can be used to
compress an image as follows. Given an RGB image, compress each of the color planes independently
and then recombine them. To compress a color plane give by the 2D matrix A, compute the SVD of
A as indicated above. Next, zero out the singular values that are smaller than some threshold value
to obtain the compressed . Finally, reconstruct the color plane by computing A = U V T .
(a) Write a function CompressImage which takes as arguments the input lename of the original RGB
image, the output lename to which you will write the compressed image, and a percentage, p.
In your compression algorithm, retain any singular value > p
max.
(b) Run your program on the le 0.00-winter.jpg. Use thresholds of 1%, 2%, ..., 10% and save the
results in les 0.01-winter.jpg, 0.02-winter.jpg,..., 0.10-winter.jpg, respectively. Modify
the script RunCompressImages.m to do this. Submit 0.01-winter.jpg, 0.05-winter.jpg and
0.10-winter.jpg.
(c) What are the sizes of the resulting compressed les? Make a plot showing threshold percentage
vs. resulting image size. You can use the command ls -l at the Matlab command prompt to
display the image les and their sizes in bytes. Save your plot as a pdf le and submit it.
(d) Write a Matlab script DisplayImages.m to display the compressed les in order starting with the
original, 1% compression, 2% compression, pausing for 0.5 seconds after displaying each image.

2. (30 points) Write a Matlab script PlaneIntersection.m to recreate the gure below, by doing the
following:
Name
Kermit
Piggy
Animal
Fozzie
Gonzo

HW1
21
21
47
55
76

HW2
51
20
14
1
68

Exam 1
76
93
24
51
14

HW3
6
5
37
57
44

HW4
16
87
3
2
18

Exam 2
69
71
26
32
64

(a) Create mesh plots of the two planes


x + y + z = 4,
x

y + z = 2,

in a single gure using the Matlab command mesh. Use x = [ 5 : 5] and y = [ 5 : 5].
(b) For x, y 2 [ 5, 5], nd the set of integer triples (x, y, z) that satisfy both plane equations. Also
have your script output these integer triples in the command window.
(c) Use scatter3 to plot these integer triples in your gure.
(d) Choose a view similar to the one shown in the example gure. Have your script automatically set
that view by generating code from the gure window and adding it to your script.

2
3. (30 points) Consider again the following table of grade data from lab 6. Write a script Lab9Problem3.m

which implements the following instructions.

(a) Load the data header, names, and data from the le datale.mat.
(b) Create a cell array containing the data called cellArray with one row per muppet and three
columns. Columns one, two and three should contain the name, an array of all the homework
scores, and an array of all the exam scores, respectively. Verify the following results:
>> cellArray{1,2}
ans =
21
51
>> cellArray{1,3}
ans =
76
69
>> cellArray{4,3}
ans =
51
32

16

(c) Run the command cellfun(@mean,cellArray(:,2)). What does this command do? What data
type is the result?
(d) Add a fourth column for the average homework score in each row, and a fth column for the
average exam score in each row. Use mean and cellfun as in the previous step. You can use the
function num2cell to write the result into the new column.
(e) Convert cellArray into a structure array, structArray, with eld names name, hw, exams, hwAvg,
and examAvg using the matlab function cell2Struct. Verify the following results:
>> structArray(1)
ans =
name: 'Kermit'
hw: [21 51 6 16]
exams: [76 69]
hwAvg: 23.5000
examAvg: 72.5000
>> structArray(5)
ans =
name: 'Gonzo'
hw: [76 68 44 18]
exams: [14 64]
hwAvg: 51.5000
examAvg: 39

CS30 Spring 2014

Extra Credit
Due: Friday, June 13, 8:00 am
Submit code for the functions you write. Submit by emailing shinar@cs.ucr.edu.

1. (40 points) Maximum sum sublist. Given a list (1D array) of integers, both positive and neg
ative,
write a function MaximumSumSublist which nds the contiguous sublist with the largest possible s
um.
Your function should return the sum of the numbers in the sublist, as well as the start and end i
ndices
of the sublist. I.e.,
function [sum, startIndex, endIndex] = MaximumSumSublist(list)
For example, the box shows the sublist giving the maximum sum and the result of calling Maximum
SumSublist.
[2, 5, 4, 3, -1, 2,

5, 4]

>> [sum, startIndex,endIndex] = MaximumSumSublist(list)


sum =
8
startIndex =
3
endIndex =
6
There are several dierent ways to solve this problem, with varying degrees of e ciency. How
many
times does your code visit each element in the list? Make a plot of list size vs. number of list ac
cesses
to see how e cient your code is.
2. (60 points) Animated bouncing circle. Use Matlab to animate the bouncing of a circle in a box.
Your
function BounceCircle should take as input the initial position and velocity of the circle, a coe
cient
of restitution, C 2 [0, 1], and an array with the extents of the box [ xmin xmax ymin ymax ].
This
is similar to the Projectile example we looked at in class and in the reading, and animated in
class.
However, in this case, you should detect when your circle hits a wall, and it should bounce o re
ecting
about the normal. The speed at which it bounces o will C v, where v is the speed at which
it hits
the wall. Also, you do not need to include gravity. Submit your function code.

You might also like