Homework #4: Conditionals

advertisement
Homework #4: Conditionals
CSE7 Winter 2016
Before beginning this homework, create a new Notepad++ file in your cs7wXX home directory on
ieng6 as in Lab#4. Save as H4_LastName.txt as in Homework #3.
To determine your cs7wXX account name, see UCSD ACMS account lookup:
https://sdacs.ucsd.edu/~icc/index.php
PART ONE: VECTORS AND CONDITIONALS
Suppose there is temperature data collected for 1000 cities on a certain day. A database just got
updated with those temperatures (in Fahrenheit) for the 1000 cities. Let us generate a variable
'temps1k' which contains random numbers with a mean of 72 and a standard deviation of 30.
1. Type the following command in the Command Window:
This will generate sample temperatures for 1000 cities with a mean of 72 and standard
deviation of 30.
2. Now generate 1000 unique ID numbers for the cities that identifies each city separately:
This will create a matrix called id1k which holds the 1,000 numbers starting at 400,001 and
ending at 401,000.
3. id1k has 1000 unique IDs and temps1k has corresponding temperature for each city. So
row 5 in 'temps1k' has temperature for city ID in row 5 of 'id1k'. In MATLAB, we can use
logical expressions to extract certain values from these matrices. For example,
§ temps1k( id1k == 400010) will search id1k for the element matching 400010
and return a vector of 1’s and 0’s corresponding to whether the logical expression
was true or false. It then uses this vector to return the elements of temps1k for
which there is a 1 (aka true) in the corresponding position of the boolean vector.
So, this command effectively returns the temperature value for city 400,010
§ id1k( temps1k >=72 & temps1k < 73) will create a matrix containing the
IDs of the cities whose temperature is 72 (since the temperature is a decimal value,
this corresponds to values 72.0000 – 72.9999)
•
•
•
•
Question #1: Write a command that generates a vector, with City IDs of cities who had
temperatures >=60. Call this vector warmCities.
Question # 2: Write a command that generates a vector, with City IDs of cities who had
temperatures <49 or >97. Call this vector extremeCities.
Question # 3: Write a command that generates a vector, with City IDs of cities who had
temperatures >=81 but <89. Call this vector mildCities.
Question # 4: Write a command that generates a vector, with City IDs of cities who had
temperatures >=49 but <64. Call this vector coldCities.
Paste the commands for each matrix into your document.
PART TWO: PLOTTING, HISTOGRAMS
•
Question # 5: Plot 'temps1k' in a histogram with 10 bins, 40 bins, 100 bins. (Save all 3
images as .jpg files)
•
Question # 6: Repeat the experiment, by generating temperatures for 5000 cities and
storing them in 'temps5k'. Plot these 5000 numbers in a histogram with 20 bins, 40 bins, 80
bins. (Save all 3 images as .jpg files)
•
Question # 7:
o Do you notice any difference in histograms (1000 vs 5000 numbers)? Write your
comments in the report.
o Does the shape of the histogram give you clue about the distribution that was used to
generate these random numbers? Write your comments in the report.
PART THREE: MORE CONDITIONALS
Write a script file (save as UCSDcolleges.m) that takes a number between 1 - 6 and displays a
word description of the UCSD college that the number corresponds to.
So the script will ask the user for a number and then returns the name of the college according to
the following scheme:
• Display ‘Error’ if the input is less than 1 or greater than 6
• Display ‘Muir’ if input is 1
• Display ‘Revelle’ if input is 2
• Display ‘Marshall’ if input is 3
• Display ‘Warren’ if input is 4
• Display ‘Roosevelt’ if input is 5
• Display ‘Sixth’ if input is 6
Some of the code is given below. Remember to replace ??? with the correct code.
Question # 8: Copy and paste your code from UCSDcolleges.m to your text file.
Homework Check off (each student individually graded):
•
Demonstrate to the TA/Tutor checking you off that you know how to use logical and relational
operators.
•
You must have 6 images (3 temps1k histograms and 3 temps5k histograms)
•
You must have UCSDcolleges.m
•
He/she will ask you questions and also check to see if you’ve done things correctly by checking the
contents of files on your cs7wXX home directory ieng6 server.
Download