How to extrapolate between two 1x1000 matrixes in an more efficient way?

2 views (last 30 days)
I have two matrixes that are dimensions 1X1000. Each matrix represents simulated revenues for one year, 1000 times per each year. Matrix A represents the year 2020 and matrix B the year 2025. I want to extrapolate between each element so that for instance the first simulation outcome in 2020 connects lineary to the first simulation outcome in 2025. So far I have coded this in this way:
% delta [2020-2025]
Delta_2020_2025CfD = zeros(1,1000);
for i =1:1000
for j=1:1
Delta_2020_2025CfD(j,i) = Aggregated_CfDRevenues2025(j,i) - Aggregated_CfDRevenues2020(j,i);
% delta per year
Year_differance = 5
Deltaperyear_2020_2025_CfD = zeros(1,1000);
for i =1:1000
for j=1:1
Deltaperyear_2020_2025_CfD (j,i) = Delta_2020_2025CfD (j,i) / Year_differance;
% extrapolated aggregated revenues [2021,2022,2023,2024]
ExAggRev_2021CfD = zeros(1,1000);
ExAggRev_2022CfD = zeros(1,1000);
ExAggRev_2023CfD = zeros(1,1000);
ExAggRev_2024CfD = zeros(1,1000);
for i =1:1000
for j=1:1
ExAggRev_2021CfD (j,i) = Aggregated_CfDRevenues2020(j,i) + Deltaperyear_2020_2025_CfD(j,i);
ExAggRev_2022CfD (j,i) = ExAggRev_2021CfD (j,i) + Deltaperyear_2020_2025_CfD(j,i);
ExAggRev_2023CfD (j,i) = ExAggRev_2022CfD (j,i) + Deltaperyear_2020_2025_CfD(j,i);
ExAggRev_2024CfD (j,i) = ExAggRev_2023CfD (j,i) + Deltaperyear_2020_2025_CfD(j,i);
%matrix revenues [2021 to 2024 inc]
Rev_CfD_2021to2024 = [ExAggRev_2021CfD; ExAggRev_2022CfD; ExAggRev_2023CfD; ExAggRev_2024CfD]
This works. However is there a smarter and more efficient way to do this? Like some Matlab function that I do not know of? I have looked at interp1 but I think this would not work in my case, since I am looking to have a fixed starting point (the revenues in 2020) and a fixed ending point (the revenues in 2024).
Thanks a lot!
P.S - I am new to Matlab :)

Accepted Answer

nathan blanc
nathan blanc on 5 Oct 2021
first of all there is no reason to use a loop with only one variable "j=1:1". you can simply write ExAggRev_2021CfD (1,i).
more importantly, in MATLAB you don't have to use loops for such operations. you can add and subtract entire matixes. this is usually much faster than adding them up in loops. in your case if you want to generate the matrix for the year x
matrix= (Aggregated_CfDRevenues2020+(x-2020)/(2025-2020)*Aggregated_CfDRevenues2025)

Sign in to comment.

More Answers (0)


Find more on General Applications 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!