Calculating annual means for different time periods

Hello,
I am trying to calculate annual means from monthly data for a number of basins with different time periods.
my data looks something like this:
time (year/month) Amazon Congo .....
195101 6430 4356
195102 2530 7960
195103 1820 2060
..... ..... .....
..... ..... .....
..... ..... .....
200610 16200 NaN
200611 6540 NaN
200612 9870 NaN
and so on. In addition I have two variables specifying the start and end years of the records for each basin as they differ from one basin to another:
start end
195101 200012
195601 199312
197101 200612
..... .....
So my questions is: how can I tell matlab to calculate the mean of each year (mean 195101-195112 and so on) and also tell it to start and end calculations at a particular year depending on the basin?
Cheers, Anna

4 Comments

Apologies, the formatting seems to have gone wrong.
so i have the following vertical columns (time, Amazon, Congo...) with some missing values:
time 195101 195102 195103 ....... 200610 200611 200612
Amazon 4950 5490 7050 ... 5490 12900 9548
Congo 3095 6904 6094 ... NaN NaN NaN
.....
and two vertical columns specifying start and end years/months
start 195101 195601 197201 ....
end 200612 199112 199912 ....
time = [ 195101; .... 200612 ]
amazon = [ 6430; . . . 9870 ]
for first year of amazon
my1 = mean(amazon(1:12,1))
for second year of amazon
my2 = mean(amazon(13:24,1)) .............. etc
your question is a little confusing but that's what I understood, sorry anything.
in vertical change to
my1 = mean(amazon(1,1:12))
for stop the mean you can use an 'for':
for n = 1:12
my1 = mean(amazon(1,n))
end
Sorry perhaps I didn't make myself very clear. So I have vertical columns (the formatting's gone very funny for some reason) for time and runoff in different basins. I have 33 basins in total so I would like to calculate the mean using a loop. I need the annual mean for every year (so for example 195101-195112, 195101-195212, 195301-195312 and so on) but the problem is the time period I want to calculate the annual means varies according to the basin. So, for example, for Amazon I want the mean for 1951-2006 but for Congo i want it for 1951-1990. How can I specify this in a loop?
As I said I have a variable for the start year (a vertical column with a value for each basin) and for the end year. I just don't know how to include this in the loop.
hope this makes sense. thanks for your help :)

Sign in to comment.

 Accepted Answer

variant
corrected [14:14 MDT]
%{
data - your array (double array) in view :
data = [195101 6430 4356
195102 2530 7960
195103 1820 2060
200610 16200 NaN
200611 6540 NaN
200612 9870 NaN]
%}
dy = datevec(num2str(data(:,1)),'yyyymm');
d1 = [dy(:,1), data(:,2:end)];
t = unique(d1(:,1));
outdata = [t, nan(numel(t),size(data,2)-1)];
for j1 = 2:size(data,2)
t2 = ~isnan(d1(:,j1));
yrs = unique(d1(t2,1));
i1 = ismember(outdata(:,1),yrs);
outdata(i1,j1) = cellfun(@mean,mat2cell(data(t2,j1),histc(d1(t2,1),yrs),1));
end

3 Comments

Hey man!, here your answer ANNA.
Hi Andrei and Eng_Amb,
thanks a lot for your answers!
I just have one more question. I ran the first line (d1=[year....) and got the following error: 'Undefined function or method 'year' for input arguments of type 'char''.
How can I get past this error? I'm not entirely sure what year is referring to, whether it is a variable or a matlab function/input argument, so it would be great if you could clarify.
Also, what does 't' refer to on the second line?
thanks again :)
You do not have a Financial Toolbox.
See corrected

Sign in to comment.

More Answers (1)

I suggest you look for John D'Errico's File Exchange contribution named consolidator.

Categories

Community Treasure Hunt

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

Start Hunting!