Homework #7:

advertisement
Homework #7:
CSE 7, Winter 2016
Before beginning this homework, create a new Notepad++, H7_LastName.txt, file in your cs7wXX home
directory.
To determine your cs7wXX account name, see UCSD ACMS account lookup:
https://sdacs.ucsd.edu/~icc/index.php
PART ONE: CONVERTING BETWEEN LOOPS
1. Let’s work a bit more with loops. We know two kinds of loops at this point, for loops and
while loops. They have a lot of things in common, and it’s important that we are fluent with
both iteration techniques. Start by creating a script called moreWhiles.m .
2. So, let’s practice converting a for loop into a while loop and vice versa. Let’s try to convert a
for loop into a while loop.
3. QUESTION #1 The code on the left is the original for loop and you have to write out the code
for a while loop (pseudo code given) so that it does the same thing as the code on left. Put
both pieces of completed code into your script file. Paste your while loop code into the text file.
k=0;
for i = 12:20
k = k-i;
end
k
Initialize k to 0
set variable i to ______
while i is less than or equal to ______
k equals to _____
increment i
end
display k
4. QUESTION #2 Now let’s do it again, only this time, let’s convert a while loop (right box) into a
for loop (left box). Enter the while-loop below into your script and convert it into a for loop
(some of the code is given to you). Paste the for-loop into your text file.
5.
score_data = [85 90 91 94 96 75
84 77 50];
set variable maxScore to ___
for i from 1 to ____
if … > …
… = …
end
end
score_data = [85 90 91 94 96 75 84
77 50];
maxScore = -1;
i = 1;
while i < length(score_data) +1
if score_data(i) > maxScore
maxScore = score_data( i);
end
i = i+1;
end
6. QUESTION #3. What is the maxScore found by the while loop/for loop? Why is it okay for us
to length in place of size (like we did in previous labs)?
PART TWO ELEMENTWISE MULTIPLICATION
1. In this week’s lab, we talked about Matrix Multiplication, and you may recall that each row of
the first matrix was multiplied by each column of the second matrix.
2. Also, if you were to multiply matrix A with dimensions [m x n] by matrix B with dimension [n x k]
the resulting matrix would have dimensions [m x k]. Remember that only the inner
dimensions had to agree (be equal). In the above example, matrix A had n columns and
matrix B had n rows.
3. This week, we are going to talk about a concept called element-wise multiplication. In matlab,
in order to multiply two matrices A and B element wise we use .* The size is emphasized so
that you can see that the operators are a period and a multiplication sign. When spoken, we
say A “dot times” B we are multiplying A and B element wise.
4. Like, last time we’ll talk about dimensions first. When multiplying two matrices element-wise,
they must have IDENTICAL dimensions. In MATLAB terms size(A) must equal size(B) if we
want to perform element-wise multiplication of A and B.
5. Let’s look at how it actually works: If we multiply A and B, we get C.
b 12 b 13
a11 b11
a12 b12
b 21 b 22 b 23
a21 b 21
a2 b 22
a31 a32 a33
b 31
a31 b 31
a32 b 32
Matrix A
Matrix B
Matrix C
Matrix A
Matrix A
a11
a11
Matrix A
a11
a12 a13
b11
a21 a22 a23
.*
a12 a13
b 32 b 33
a12 a13
a11
a13 b 13
2
a23 b 23
a33 b 33
a12 a13
6. Notice that each A B and C all have the same dimensions. In this example, they are all 3x3.
7. Let’s create our own simple example so that we can see what’s going in matlab
8. Create a script called elementWise.m and type the following
A = [3 3 3; 3 3 3; 3 3 3];
B = [1 2 3; 4 5 6; 7 8 9];
A
.*
B
9. QUESTION #4: What is the resulting matrix? What do you notice about the output in
comparison to B? What happens if you change the last line to B .*A? Does it work the same
way? Is element-wise multiplication commutative (look this up if you don’t remember what it
means)?
PART THREE ELEMENTWISE MULTIPLICATION IN ACTION
1. So now let’s consider some applications where we might want to use element-wise
multiplication.
2. Create a new script called convert_measurements.m
3. Imagine that we are scientists working internationally, and for our work, we need to use
measurements taken by an American team.
4. The American team foolishly used the English system for all of their measurements, and so we
have to convert each measurement individually from gallons to liters, inches to meters, and lbs
to kilograms.
5. Go ahead and download the file called american_measurement.csv from the class webpage
and save it to your MATLAB folder.
6. Use the following command to store the values in variable called amer_data using the
following command
amer_data = load(‘american_measurement.csv’);
7. We now have 100 of these measurements in file that looks like this.
Gallons
6.2
Inches
16.3
Lbs
8.8
8. And we want to convert the whole set of measurements to the metric system so that the new
matrix looks like
Liters
23.4695
Meters
0.4140
Kilograms
3.9916
9. HINT: 1 gallon = 3.78541 liters, 1 inch = 0.0254 m, 1 lb = 0.4535 kilograms
10. There are 2 ways to do this. The first is with a loop, and general idea is that we loop over each
of the 10000 rows of amer_data and then build a new matrix called converted where each row
of converted is a row of amer_data multiplied element-wise with a vector.
converted_mat = zeros(size(amer_data));
create a vector conversion_vector to store your conversion factors (look at
Lab 7 if you forgot)
for each row in amer_data
do converted_mat(i, :) = amer_data(i , : ) .* conversion_vector
11. Pseudo code is given above, implement the script convert_measurements.m and paste your
code and the first row of your converted matrix into Question #5
12. Create a new script called convert_measurements2.m copy and paste your code and the first
row of your converted matrix to Question #6
13. The second approach makes use of a cool matlab function called repmat that takes your
conversion vector, copies it so that its dimensions matches the dimension of amer_data and
then you can simply element wise multiply the two.
converted_mat = repmat conversion_vector
converted_mat = elementwise multiplication of converted_mat and amer_data
14. If you can’t figure out how to use repmat type doc repmat or help repmat at the command
prompt. The basic idea is that it replicates (or copies) a matrix some number of times
A = [1 2 3]
repmat(A,3,1)
ans =
1 2 3
1 2 3
1 2 3
15. Try running both scripts convert_measurements.m and convert_measurements2.m and
see if you get the same results.
PART FOUR: SAVING AND SUBMITTING
SUBMIT *FOUR* THINGS:
1. Save your Notepad++ file.
2. moreWhiles.m
3. elementWise.m
4. convert_measurements.m
5. convert_measurements2.m
HOMEWORK #7

You will be graded individually.

You will need to show the Tutor/TA that you can run your convert measurements script

And you will be expect to explain how for/while loops work and how to convert between
them

You should have one file named “Hw7_LastName”, saved in your cs7wXX home directory.
(See Homework # 1 details)
Download