Path: news.mathworks.com!not-for-mail
From: "Branko " <bogunovic@mbss.org>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Summarizing yearly/monthly data in time series
Date: Thu, 29 Oct 2009 11:00:18 +0000 (UTC)
Organization: National Institute of Biology
Lines: 44
Message-ID: <hcbsk2$4va$1@fred.mathworks.com>
References: <hcaam2$8dv$1@fred.mathworks.com> <hcac7v$3d5$1@news.eternal-september.org> <c28afeaa-2a30-4cdd-bffc-07aceb07a1f4@h40g2000prf.googlegroups.com> <hcbktu$jjv$1@fred.mathworks.com>
Reply-To: "Branko " <bogunovic@mbss.org>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1256814018 5098 172.30.248.37 (29 Oct 2009 11:00:18 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 29 Oct 2009 11:00:18 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 237386
Xref: news.mathworks.com comp.soft-sys.matlab:580909


"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <hcbktu$jjv$1@fred.mathworks.com>...
> > Now come on children, stop bickering!!
> > 
> > It's quite easy really.
> > To get the start times for the first of each month for the 100 months
> > from 1-Jan-2009:
> > t=datenum(2009*ones(100,1),[1:100]',ones(100,1));
> 
> well show me how easy is to get the intervals on each end of the month ona period longer than 4 years...
> :)

Here is one approach:

% Time span and increament
start = datenum('1-1-2004');
increment = datenum([0 0 1 0 0 0]); % day
finish = datenum('12-31-2009');
time = start:increment:finish;
yr=str2num(datestr(time,'yyyy'));
mon=str2num(datestr(time,'mm'));
day=str2num(datestr(time,'dd'));

% Sort by year
[vu,ix,ix]=unique(yr);
n=accumarray(ix,1);
nc=arrayfun(@(x) 1:x,n,'uni',false);
nc=[nc{:}].';
y=(accumarray([ix,nc],day,[],[],nan))';
Y=y(:)';
Y(isnan(Y))=[];

% Find max of the month
idx=[false,Y(1:end-2)<Y(2:end-1) & Y(2:end-1)>Y(3:end),false];
% Plot every fourth month
x=(1:length(Y))';
line(time,Y);
line(time(idx),Y(idx),'marker','*','Color','g','linestyle','none');
datetick('x','mm-dd-yy');
ylabel('Days in moth');
xlabel('Time')
% Display
disp([time(idx); Y(idx)]');

Branko