Calculating the average value for each year
5 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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)];
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!