필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by