How to add column values of different tables based on dates column
5 views (last 30 days)
Show older comments
Hello, I have a table of daily stock prices by date:
Date: Price
01/01/2010 50.6
02/01/2010 51.2
... ...
and another table with dividends by their payment date:
Date: Dividend:
02/01/2010 2.5
02/05/2010 1.6
... ...
I want to adjust the prices with the paid dividends by checking whether a dividend was paid for every Date in table1 and where the Date in table1 and table2 match, I want to overwrite the price in table1 to equals = Price.table1 + Dividen.table2 for that specific date.
So the end result should be:
Date: Price:
01/01/2010 53.1
02/01/2010 51.2
... ...
I know this can be done with a for and if loop, I just can't get my head around it.
Please advise
0 Comments
Accepted Answer
Star Strider
on 6 Mar 2018
If you have R2016b or later, either the retime (link) or synchronize (link) function will probably work.
4 Comments
Star Strider
on 6 Mar 2018
I appreciate your update.
I can only direct you to what appear to me are appropriate ways to solve your problem, from your description of it and your description of your data.
More Answers (1)
Peter Perkins
on 8 Mar 2018
I think 'sum', as in "the input to retime or synchronize", is a red herring here. That parameter sums across time. Assuming you've made two timetables, I think what you want is something like
ttDividends = retime(ttDividends,ttProces.Time,'FillWithConstant','Constant',0)
ttPrices.Price = tt.Price + ttDividends.Dividend
But it's probably useful to combine prices and dividends. Assuming the dividend dates are a subset of the price dates, this
ttCombined = synchronize(ttProcess,ttDividends,'first')
will NaN-fill the dates with no dividends. Then
hadDividend = ~isnan(ttCombined.Dividend);
ttCombined.NewVarName(i) = ttCombined.Price(i) + ttCombined.Dividend(i)
0 Comments
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!