MATLAB: Count of months spanned

countdatenumdatevecmonths

I can't think of a way to do this without a loop. I have a bunch of start and end dates. Each row is a record and I want to be about to count how many months are in each date range. Here's some sample data startdates=[735406;735416;735404;735396;735363;735389]; enddates=[735433;735433;735433;735425;735416;735416];
So Jun 21-Jul 18 would be two months
>>countmonths =
2 1 2 2 3 2
My way is not very efficient
for i=1:length(startdates)
daterange=startdates(i):enddates(i);
dv=datevec(daterange);
countmonths(i)=size(unique(dv(:,[1 2]),'rows'),1);
end

Best Answer

Hi Leah,
I think you can do this quite nicely as follows:
startvecs = datevec(startdates);
endvecs = datevec(enddates);
diffvecs = endvecs - startvecs;
countmonths = 12*diffvecs(:,1) + diffvecs(:,2)+1
Does that work for you? I'm not quite sure what you'd want to do between, say, Jan1 and Feb1, but I think if you take a look at diffvecs you'll understand what it contains and be able to make it work for your purposes.