Write a matlab function The high temperature for each month for a year (in Fahre
ID: 3750819 • Letter: W
Question
Write a matlab function
The high temperature for each month for a year (in Fahrenheit, rounded to integers) for different locations are stored in a file. Each line of the file has a location ID, followed by 12 temperatures. For example, the file might store:
432 33 37 42 45 53 72 82 79 66 55 46 41
777 29 33 41 46 52 66 77 88 68 55 48 39
567 55 62 68 72 75 79 83 89 85 80 77 65
The value on the first field in each row (432, 777 and 567 in this example) is a location ID. The 12 numbers that follow the location ID are the high temperatures for the months. Write a Matlab function computeMaxTemperature, that will take the filename as an input argument, compute the maximum temperature for each location, and return two row vectors: the first vector must contain the location IDs in the same order they appear in the file; and the second vector must contain the top temperature for each location (the same order as the locations). You must use the computeMatrixMax function from the previous example. If the specified file does not exist, the function must return -1 for both the location ID and the maximum temperature. If the specified file exists, you can assume that the data in the file has the correct format. Your function will have the following signature:
function [loc_id max_temp] = computeMaxTemperature(file_name)
Inputs: file_name - name of the file that contains the data. There has two file name data1.dat and data2.dat
Output: loc_id - 1 x N vector with location IDs, in the same order they appear in the file - N is the number of rows in the file
-1 if the file does not exist
max_temp - 1 x N vector that contains the maximum temperature for each location
-1 if the file does not exist
In the above example, if the file specified above was given as the input file name, the output must be:
loc_id = [432 777 567]
max_temp = [82 88 89]
Explanation / Answer
matlab script:--
function [loc_id, max_temp] = computeMaxTemperature(file_name) % function with input file name
if ~exist(file_name) % check file name not exist
loc_id=-1; % location vector=-1
max_temp=-1; % max_temp=-1
else % else
raw_data=load(file_name); % load file as raw data matrix
loc_id=raw_data(:,1); % assign locations to loc_id
temperature=raw_data(:,2:end); % assign temp values to temperature
max_temp=max(temperature,[],2); % max values from each row of temperature
loc_id=loc_id'; % for row vector of loc
max_temp=max_temp'; % for row vector of temp
end
end
output:--
>> [loc_id, max_temp] = computeMaxTemperature('location_temp.txt')
loc_id =
432 777 567
max_temp =
82 88 89
>> [loc_id, max_temp] = computeMaxTemperature('location_temp1.txt')
loc_id =
-1
max_temp =
-1
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.