Calculating the average value for each year

8 Ansichten (letzte 30 Tage)
Jason
Jason am 26 Okt. 2014
Beantwortet: Andrei Bobrov am 26 Okt. 2014
I'm a student learning matlab and I'm having trouble calculating averages. The first six columns of the array "data2" are the datevector values; the 7th column contains average temperature for each month, from 1895 to 2006 (as shown in the attached "Final project - FortStJames arrays" file). I'm trying to calculate the average temperature for each year.
I think I need two loops (one for the year and another one to loop over all the temperature values). I'm not sure which should be the outer and inner loops. I'm also not sure what indices to use in this part of the loop
temp_avg(i,1) = mean( data2(,) );
Here is my code:
% data2 contains the datevector and a 7th column for the temp
data2 = my_datevectors;
data2(:,7) = temp4;
temp_avg = []; % we'll fill in the values
% Use a loop to compute the average temp for each year
for i = 1:length(data2) % length(data2) is # of observations
for j = 1895:2006 % j is the year
temp_avg(i,1) = mean( data2(,) );
j = j+1;
end % end of loop2
end % end of loop1
Thanks

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 26 Okt. 2014
I use Import Data from MATLAB menu (Home):
[yy,~,c] = unique(FortStJamesv22{18:end,'Year'});
out = [yy, accumarray(c,FortStJamesv22{18:end,'MeanTempC'},[],@nanmean)];

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by