How to add column values of different tables based on dates column
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2018년 3월 6일
If you have R2016b or later, either the retime (link) or synchronize (link) function will probably work.
댓글 수: 4
Star Strider
2018년 3월 6일
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.
추가 답변 (1개)
Peter Perkins
2018년 3월 8일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!