Calculating the average value for each year
4 次查看(过去 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 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!