How to add column values of different tables based on dates column
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
Accepted Answer
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)
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!