Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Summarizing yearly/monthly data in time series
Date: Wed, 28 Oct 2009 21:54:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <hcaehq$833$1@fred.mathworks.com>
References: <hcaam2$8dv$1@fred.mathworks.com> <hcac7v$3d5$1@news.eternal-september.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1256766842 8291 172.30.248.35 (28 Oct 2009 21:54:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 28 Oct 2009 21:54:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1886545
Xref: news.mathworks.com comp.soft-sys.matlab:580786


dpb <none@non.net> wrote in message <hcac7v$3d5$1@news.eternal-september.org>...
> Ryan Utz wrote:
> > Hi all,
> > 
> > I have a time series dataset that consists of daily data over several
> > years. Most of the analyses are run on a matrix where the first
> > column is the year, the second the month, the third the day and the
> > final column is the data of interest. I want to do some simple
> > summary statistics (sum of the data, for instance) for each month. I
> > was assuming I'd do this in a for-loop.
> > 
> > Normally, I would identify each unique month using the 'unique' and
> > 'find' commands  in the for-loop but since there is a month AND year
> > I'm not quite sure how to do it. I have figured out that 'find' may
> > include conditionals (i.e. '&') but still can't get to the bottom of
> > it...
> > 
> > Also, I know I can use the 'consolidator' macro to do this quite
> > easily, but I'm trying to write my own script for use by others and
> > don't want to make the users download 'consolidator' as well.
> > 
> > Any takers?
> 
> I'd suggest turning the y/m/d columns into date numbers and then use 
> date ranges on them instead of trying to do it on individual values.
> 
> --
Data ranges loose on precision.
% I suppose your matrix would look like this:
[Y, M, D] = datevec((733075:734074)');
A = [Y, M, D , -1.7 + (5).*rand(1000,1)];

% Retrieve unique combinations of year and month
Monthly = unique(A(:,1:2),'rows');
% Build subs for accumarray
[~, subs] = ismember(A(:,1:2), Monthly, 'rows');
% Do accumarray
Monthly_sum = accumarray(subs, A(:,4));