I tried to use datenum to have serial consequent number that are missing when there are festivities but i can't continue at the moment.
How to insert zeros in a data returns
5 ビュー (過去 30 日間)
古いコメントを表示
I have a data with two variables: dates and returns. These two variables don't include the festivities, i'd like to insert zeros in the returns in those days. Is there anyone who can help me?
12 件のコメント
採用された回答
Adam Danz
2019 年 6 月 5 日
編集済み: Adam Danz
2019 年 6 月 5 日
For r2016b or later
Assuming your dates are a cell array of strings in the format you described in the comments under your question, you can put your data into a timetable, then use retime() to fill in the missing dates.
Dates = {'2013-11-01', '2013-11-02', '2013-11-05', '2013-11-06'};
Returns = [1.05, 1.03, 1.05, 1.03];
% Convert dates to datetime
t = datetime(Dates,'InputFormat','yyyy-MM-dd');
% put dates and data into timetable
tt = timetable(t',Returns','VariableName',{'Returns'});
% expand your time table to include all missing days; fill missing Returns with 0
ttFill = retime(tt,'daily','fillwithconstant','Constant',0);
% Extract the vector (if you must)
Finalreturnmatrix = ttFill.Returns;
Results
ttFill =
6×1 timetable
Time Returns
___________ _______
01-Nov-2013 1.05
02-Nov-2013 1.03
03-Nov-2013 0
04-Nov-2013 0
05-Nov-2013 1.05
06-Nov-2013 1.03
>> Finalreturnmatrix
Finalreturnmatrix =
1.05
1.03
0
0
1.05
1.03
Lower level version (for earlier releases)
'allReturns' is the vector you're looking for. This approach doesn't rely on datetime, timetables, etc.
Dates = {'2013-11-01', '2013-11-02', '2013-11-05', '2013-11-06'};
Returns = [1.05, 1.03, 1.05, 1.03];
alldates = datestr(datenum(Dates{1},'yyyy-mm-dd') : 1 : datenum(Dates{end},'yyyy-mm-dd'),'yyyy-mm-dd');
allReturns = zeros(size(alldates,1),1);
allReturns(ismember(alldates,Dates)) = Returns;
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Time Series Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!