How to insert zeros in a data returns

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 Comments

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.
Could you provide an example of what you'd like to do?
Lbuni
Lbuni on 5 Jun 2019
Edited: Lbuni on 5 Jun 2019
Dates(3x1)=[2013-11-01, 2013-11-02, ( jump saturday and sunday), 2013-11-05]
And these are the related returns for these days:
Returns(3x1)=[1.05, 1,03, (jump), 1.05]
Finalreturnmatrix(5x1)=[1.05, 1.03, 0, 0, 1.05]
Consider that my data jumps also other festivities (like christmas etc..).
I need only a vector like "finalreturnmatrix", the vector of the dates is not required to be completed. I hope it's more understandable now. Thank you
Yes, it's more understandable.
What format are you "Dates"? Are they datetime? Are they strings?
class(Dates) = ?
"cells" or "double" if i import them as a date/time. At the moment it says :
"Undefined function 'datetime' for input arguments of type 'double'.
Error in changeforma (line 2)
t = datetime(Dates,'InputFormat','yyyy-MM-dd');"
Thanks for your patience.
If you import them as datetime, you can name that variable 't' (for now) and skip this line of code from my answer:
t = datetime(Dates,'InputFormat','yyyy-MM-dd');
If you continue to have problems, please attach a sample of data instead of me guessing what you're working with.
I've got the r2014a version of matlab, it seems that i can't use the functions that you wrote before. There aren't in my documentation. Anyway i uploaded a datasample, there are prices instead returns but it's the same thing.
The csv file is corrupted (when I opened it it was gibberish). Anyway, see my updated answer; at the bottom is a low-level solution that doesn't involve any newer matlab features.
When i use your code as in your example it works but, when i use it with all of my data, it says:
"In an assignment A(I) = B, the number of elements in B and I must be the same.
allReturns(ismember(alldates,Dates)) = Returns;"
Consider that the variables have the same dimensions. I also uploaded another sample in a different format if it's useful
I just updated my answer to fix a mistake in my date formats. I used capital MM (minutes) instead of lowercase mm (months). Fixed (I tested it with your data).
It works! I really appreciated your help, good job.

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 5 Jun 2019
Edited: Adam Danz on 5 Jun 2019
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;

More Answers (0)

Products

Release

R2014b

Asked:

on 5 Jun 2019

Edited:

on 5 Jun 2019

Community Treasure Hunt

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

Start Hunting!