SOS: How to use datenum for a vector ??

11 ビュー (過去 30 日間)
Zoe
Zoe 2011 年 6 月 10 日
today = datenum('20110609','yyyymmdd');
today =
734663
D = 20110609;
d = datenum('D','yyyymmdd')
d =
734504
Why cannot the above code work ???????????????????
d1 = datenum('20110610','yyyymmdd');
d2 = datenum('20110716','yyyymmdd');
d2-d1 % it gives me 36, number of days between these two dates
HOWEVER, if I have a vector of time D(N,1), N is more than 100 K.
It looks like this: (and not sorted)
maturity =
[20110618
20110619
20110716
20110718
20110719
20111217
...];
And I wanna to get a vector of tenor, from the date in the vector to today's date: 20110610.
How may I do that? Obviously I cannot type them in...
Any experts, plz help! Thx in advance!

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 6 月 10 日
a=[20110711 20110712 20110712];
b=datenum(arrayfun(@num2str,a,'un',0),'yyyymmdd');
tenor=b-datenum('20110610','yyyymmdd')
  4 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 6 月 10 日
Great. Thanks Walter.
@Zoe, follow Wlater's improvement
a=[20110711 20110712 20110712];
tenor=datenum(num2str(a'),'yyyymmdd')-datenum('20110610','yyyymmdd');
What does "tenor" mean?
Zoe
Zoe 2011 年 6 月 10 日
days to maturity (expiration - current)
It's a term used in derivative contract.
Thx~

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 6 月 10 日
datenum('D') attempts to convert the string "D" to a date. With all the various default values that are applied when fields are missing, it comes out as being Jan 1 of the current year.
datenum(D) would use the value of the variable D. However, it is not valid to pass a scalar integer to datenum() to be converted.
datenum(cellstr(num2str(maturity)),'YYMMDD') would do your entire vector.
  2 件のコメント
Zoe
Zoe 2011 年 6 月 10 日
Thanks a lot :)
Fangjun Jiang
Fangjun Jiang 2011 年 6 月 10 日
It reminds me the cellstr() function. Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by