count of months spanned

조회 수: 5 (최근 30일)
Leah . 2013년 8월 15일
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)
countmonths(i)=size(unique(dv(:,[1 2]),'rows'),1);

채택된 답변

Sven 2013년 8월 15일
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.
  댓글 수: 1
Leah 2013년 8월 19일
Thanks, this does work for me. Jan1 and Feb1 would still count as two months, which is correct for my purposes. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan 2013년 8월 15일
편집: Jan 님. 2013년 8월 15일
When you convert the serial date numbers to date vectors you get e.g.:
s = [2013 06 21 12 13 14; ...
2013 06 21 1 2 3]
e = [2013 07 18 12 13 14; ...
2014 06 21 1 2 3]
Now the distance in months is:
(e(:, 1) * 12 + e(:, 2)) - (s(:, 1) * 12 + s(:, 2)) + 1
Or more compact:
(e(:, 1:2) - s(:, 1:2)) * [12; 1] + 1
  댓글 수: 1
Leah 2013년 8월 19일
Thanks Jan! This was really similar to the first post they both work great

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by