% Load the temperature data
data = readtable( 'temperature.dat' , 'Delimiter' , ',' , 'ReadVariableNames' , false ) ;
% Extract year and monthly temperatures
years = data.Var1 ; % First column ( Year)
monthly_temps = data{ :, 2 : 13 } ; % Columns 2 to 13 ( Jan to Dec)
% ( a, b) Find the warmest month and year
[ max_temp, max_index] = max( monthly_temps( : ) ) ;
[ year_index, month_index] = ind2sub( size( monthly_temps) , max_index) ;
p6a = month_index; % Month ( 1 for Jan, ..., 12 for Dec)
p6b = years( year_index) ; % Year
% ( c) Count occurrences of temperatures > 75 °F between 1900 and 2000
count_above_75 = sum( monthly_temps( years >= 1900 & years <= 2000 , : ) > 75 , 'all' ) ;
p6c = count_above_75;
% ( d) Count occurrences of temperatures > 75 °F in summer ( Jun, Jul, Aug) between 1900 and 2000
summer_months = monthly_temps( years >= 1900 & years <= 2000 , 6 : 8 ) ; % Columns for Jun, Jul, Aug
count_summer_above_75 = sum( summer_months > 75 , 'all' ) ;
p6d = count_summer_above_75;
% ( e) Compute the annual cycle ( average temperature for each month)
annual_cycle = mean( monthly_temps, 1 ) ; % Average for each month
figure( 3 ) ;
bar( 1 : 12 , annual_cycle) ;
title( 'Annual Cycle of Temperature' ) ;
xlabel( 'Months (1 = Jan, 2 = Feb, ..., 12 = Dec)' ) ;
ylabel( 'Average Temperature (°F)' ) ;
p6e = 'See figure 3' ;
% ( f) Identify the month with the warmest average temperature
[ max_avg_temp, max_avg_month_index] = max( annual_cycle) ;
p6f
= sprintf ( 'On average, the temperature peaks in the month of %d.' , max_avg_month_index
) ;
% ( g) Write the annual cycle to a file named annual.cycle .dat
annual_data = [ ( 1 : 12 ) ', annual_cycle' ] ;
writetable( array2table( annual_data) , 'annual.cycle.dat' , 'WriteVariableNames' , false ) ;
p6g = evalc( 'type("annual.cycle.dat")' ) ; % Display the content of the file
% ( h) Compute the annual mean temperature
annual_mean_temp = mean( monthly_temps, 2 ) ; % Average for each year
figure( 4 ) ;
hold on;
plot( years, annual_mean_temp, 'k-' , 'LineWidth' , 2 ) ; % Annual mean temperature
scatter( years( annual_mean_temp > 65 ) , annual_mean_temp( annual_mean_temp > 65 ) , 'ro' ) ; % Years warmer than 65 °F
scatter( years( annual_mean_temp < 60 ) , annual_mean_temp( annual_mean_temp < 60 ) , 'bd' ) ; % Years cooler than 60 °F
title( 'Annual Mean Temperature Variation Over the Years' ) ;
xlabel( 'Year' ) ;
ylabel( 'Mean Temperature (°F)' ) ;
legend( 'Mean Temperature' , 'Temp > 65°F' , 'Temp < 60°F' ) ;
p6h = 'See figure 4' ;
% ( i) Determine the trend in temperature over the years
if mean
( annual_mean_temp
) > mean
( annual_mean_temp
( 1 : floor ( end
/ 2 ) ) ) % Compare first and second half of data
p6i = 'On average, the temperature in San Diego gets warmer over the years.' ;
else
p6i = 'On average, the temperature in San Diego gets cooler over the years.' ;
end
% Display results
disp( [ 'Warmest Month: ' , num2str( p6a) ] ) ;
disp( [ 'Warmest Year: ' , num2str( p6b) ] ) ;
disp( [ 'Count > 75°F (1900-2000): ' , num2str( p6c) ] ) ;
disp( [ 'Count > 75°F in Summer (1900-2000): ' , num2str( p6d) ] ) ;
disp( p6e) ;
disp( p6f) ;
disp( p6g) ;
disp( p6h) ;
disp( p6i) ;
JSBMb2FkIHRoZSB0ZW1wZXJhdHVyZSBkYXRhCmRhdGEgPSByZWFkdGFibGUoJ3RlbXBlcmF0dXJlLmRhdCcsICdEZWxpbWl0ZXInLCAnLCcsICdSZWFkVmFyaWFibGVOYW1lcycsIGZhbHNlKTsKCiUgRXh0cmFjdCB5ZWFyIGFuZCBtb250aGx5IHRlbXBlcmF0dXJlcwp5ZWFycyA9IGRhdGEuVmFyMTsgJSBGaXJzdCBjb2x1bW4gKFllYXIpCm1vbnRobHlfdGVtcHMgPSBkYXRhezosIDI6MTN9OyAlIENvbHVtbnMgMiB0byAxMyAoSmFuIHRvIERlYykKCiUgKGEsIGIpIEZpbmQgdGhlIHdhcm1lc3QgbW9udGggYW5kIHllYXIKW21heF90ZW1wLCBtYXhf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