フィルターのクリア

Calculating the average value for each year

1 回表示 (過去 30 日間)
Jason
Jason 2014 年 10 月 26 日
回答済み: Andrei Bobrov 2014 年 10 月 26 日
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

回答 (1 件)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by