Update values in a timetable with values and matching dates from a different timetable.
4 views (last 30 days)
Show older comments
Hello everyone, I've this problem and I just can't seem to figure it out yet.
Lets say I have a timetable like this:
cashflowdates1 = datetime({'2022-02-18';'2022-07-09';'2023-01-09'});
cashflows1=[-0.542;0.25;10];
TT=timetable(cashflowdates1,cashflows1)
This is just like output of the cashflows function for a fixedbond instrument in the financial toolbox. That first negative number represents accrued interest.
Since I want to compute the rate of return for an investment, I need to replace that number with the full price I pay for the bond.
Simple enough, right?
initialinvestment=-3.3;
TT{1,1}=initialinvestment
And now I can compute the return I wanted.
xirr(TT.cashflows1,TT.cashflowdates1,0.1,5,1)
Now, here's my problem. I would like to do this for multiple cashflows and multiple prices and dates.
So my data actually now looks like this:
cashflowdates1 = datetime({'2022-02-18';'2022-02-19';'2022-02-20';'2022-07-09';'2023-01-09'});
cashflows1=[-0.542;0;0;0.25;10];
cashflows2=[0;-0.562;0;0.25;10];
cashflows3=[0;0;-0.58;0.25;10];
TT=timetable(cashflowdates1,cashflows1,cashflows2,cashflows3)
initialinvestment=[-3.3;-3.4;-3.2];
initialinvestmentdates=datetime({'2022-02-18';'2022-02-19';'2022-02-20'});
initialinvestmentTT=timetable(initialinvestmentdates,initialinvestment)
Is there a way to update the cashflow timetable with the data of the initialinvestment timetable?
2 Comments
Ive J
on 19 Feb 2022
Why do you have several cashflows variables in table TT? Wouldn't it be easier to merge all?
dts = datetime({'2022-02-18';'2022-02-19';'2022-02-20';'2022-07-09';'2023-01-09'});
cashflow = [-0.542; -0.562; -0.58; 0.25; 10];
ttab = timetable(dts, cashflow, 'DimensionNames', {'date', 'Variables'})
initinvest = [-3.3; -3.4; -3.2];
initdts = datetime({'2022-02-18';'2022-02-19';'2022-02-20'});
itab =timetable(initdts, initinvest, 'DimensionNames', {'date', 'Variables'})
[idx1, idx2] = ismember(ttab.date, itab.date);
ttab.cashflow(idx1) = itab.initinvest(idx2(idx1));
disp(ttab)
Answers (1)
Sufiyan
on 21 Dec 2023
Hello Max,
You can refer to the code below.
cashflowdates1 = datetime({'2022-02-18';'2022-02-19';'2022-02-20';'2022-07-09';'2023-01-09'});
cashflows1 = [-0.542; 0; 0; 0.25; 10];
cashflows2 = [0; -0.562; 0; 0.25; 10];
cashflows3 = [0; 0; -0.58; 0.25; 10];
TT = timetable(cashflowdates1, cashflows1, cashflows2, cashflows3)
initialinvestment = [-3.3; -3.4; -3.2];
initialinvestmentdates = datetime({'2022-02-18'; '2022-02-19'; '2022-02-20'});
initialinvestmentTT = timetable(initialinvestmentdates, initialinvestment)
for i = 1:length(initialinvestment)
% Find the index of the minimum value in each column of cash flows
[~, minIndex] = min(TT{1:end, i});
TT{minIndex, i} = initialinvestment(i);
end
TT
xirr_result = zeros(length(initialinvestment), 1);
for i = 1:length(initialinvestment)
xirr_result(i) = xirr(TT{:, i}, TT.Properties.RowTimes, 0.1, 5, 1);
end
xirr_result
Hope it helps!
0 Comments
See Also
Categories
Find more on Tables 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!