is there a jump of 2 months?

1 回表示 (過去 30 日間)
antonet
antonet 2012 年 8 月 17 日
Dear all
I have the following sequence of dates
A={
'24/09/2000'
'22/10/2000'
'19/11/2000'
'17/12/2000'
'14/01/2001'
'11/02/2001'
'11/03/2001'
'08/04/2001'
'03/06/2001'
'01/07/2001'
'29/07/2001'
'26/08/2001'
'23/09/2001'
'21/10/2001'}
I want to see if I have a jump of two months. for instance, in the above example there is such a jump from '08/04/2001' to '03/06/2001'.
I am looking for an if statement
if there is not such a jump
'...'
end
thanks

採用された回答

José-Luis
José-Luis 2012 年 8 月 17 日
編集済み: José-Luis 2012 年 8 月 17 日
Assuming your data is ordered, and that you have the financial toolbox:
numDate = datenum(A,'dd/mm/yyyy');
numDate = numDate - numDate(1);
monthNumber = month(test) + 12 * year(test);
jumpMonth = [0; diff(monthNumber)];
The vector jumpMonth will contain the difference in months between two succesive dates.
Cheers!

その他の回答 (2 件)

Wayne King
Wayne King 2012 年 8 月 17 日
The basic way to do it is using datenum()
B = datenum(A,'dd/mm/yyyy');
numdays = diff(B);
nummonths = numdays/30;
Then you need to use floor(), round(),ceil() or something similar, but you will see that only one element of nummonths is close to 2 in value
So, in this example
nummonths = ceil(nummonths);
for nn = 1:length(nummonths)
if (nummonths(nn) > 1.5 & nummonths(nn) < 3.5)
disp('There''s a jump');
else
disp('No Jump');
end
end
Anyway, you get the point.

Andrei Bobrov
Andrei Bobrov 2012 年 8 月 17 日
[m,m] = datevec(A,'dd/mm/yyyy');
test = [0;mod(diff(m),12)] == 2;

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by