How to extrapolate between two 1x1000 matrixes in an more efficient way?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mak Dukan
am 5 Okt. 2021
Kommentiert: Mak Dukan
am 5 Okt. 2021
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);
end
end
% 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;
end
end
% 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);
end
end
%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!
Mak
P.S - I am new to Matlab :)
0 Kommentare
Akzeptierte Antwort
Nathan Bblanc
am 5 Okt. 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
x=2021
matrix= (Aggregated_CfDRevenues2020+(x-2020)/(2025-2020)*Aggregated_CfDRevenues2025)
2 Kommentare
Kevin Holly
am 5 Okt. 2021
Bearbeitet: Kevin Holly
am 5 Okt. 2021
First off, let's eliminate all the for loops. I commented out lines to remove with "% %".
% 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);
% % end
% % end
Delta_2020_2025CfD(1,1:1000) = Aggregated_CfDRevenues2025(1,1:1000)-Aggregated_CfDRevenues2020(1,1:1000);
% 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;
% % end
% % end
Delta_2020_2025CfD(1,1:1000) = Delta_2020_2025CfD(1,1:1000)/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);
% % end
% % end
ExAggRev_2021CfD (1,1:1000) = Aggregated_CfDRevenues2020(1,1:1000) + Deltaperyear_2020_2025_CfD(1,1:1000);
ExAggRev_2022CfD (1,1:1000) = ExAggRev_2021CfD (1,1:1000) + Deltaperyear_2020_2025_CfD(1,1:1000);
ExAggRev_2023CfD (1,1:1000) = ExAggRev_2022CfD (1,1:1000) + Deltaperyear_2020_2025_CfD(1,1:1000);
ExAggRev_2024CfD (1,1:1000) = ExAggRev_2023CfD (1,1:1000) + Deltaperyear_2020_2025_CfD(1,1:1000);
Now, I noticed you preallocated variables, which revealed the size of these variables. It shows you are performing actions on the entire vector. In that case, you do not need to speficify a domain nor range. This can be simplified to the below. Aggregated_CfDRevenues2025 could also be reduced if the whole vector is being used.
% delta [2020-2025]
Delta_2020_2025CfD = Aggregated_CfDRevenues2025(1,1:1000)-Aggregated_CfDRevenues2020(1,1:1000);
% delta per year
Year_differance = 5;
Delta_2020_2025CfD = Delta_2020_2025CfD/Year_differance;
% extrapolated aggregated revenues [2021,2022,2023,2024]
ExAggRev_2021CfD = Aggregated_CfDRevenues2020(1,1:1000) + Deltaperyear_2020_2025_CfD;
ExAggRev_2022CfD = ExAggRev_2021CfD + Deltaperyear_2020_2025_CfD;
ExAggRev_2023CfD = ExAggRev_2022CfD + Deltaperyear_2020_2025_CfD;
ExAggRev_2024CfD = ExAggRev_2023CfD + Deltaperyear_2020_2025_CfD;
%matrix revenues [2021 to 2024 inc]
Rev_CfD_2021to2024 = [ExAggRev_2021CfD; ExAggRev_2022CfD; ExAggRev_2023CfD; ExAggRev_2024CfD];
Assuming Aggregated_CfDRevenues2020 is a vector with a length of 1000 elements.
% delta [2020-2025]
Delta_2020_2025CfD = Aggregated_CfDRevenues2025-Aggregated_CfDRevenues2020;
% delta per year
Year_difference = 5;
Delta_2020_2025CfD = Delta_2020_2025CfD/Year_difference;
% extrapolated aggregated revenues [2021,2022,2023,2024]
ExAggRev_2021CfD = Aggregated_CfDRevenues2020 + Deltaperyear_2020_2025_CfD;
ExAggRev_2022CfD = ExAggRev_2021CfD + Deltaperyear_2020_2025_CfD;
ExAggRev_2023CfD = ExAggRev_2022CfD + Deltaperyear_2020_2025_CfD;
ExAggRev_2024CfD = ExAggRev_2023CfD + Deltaperyear_2020_2025_CfD;
%matrix revenues [2021 to 2024 inc]
Rev_CfD_2021to2024 = [ExAggRev_2021CfD; ExAggRev_2022CfD; ExAggRev_2023CfD; ExAggRev_2024CfD];
We can reduce further:
Year_difference = 5;
Delta_2020_2025CfD =(Aggregated_CfDRevenues2025-Aggregated_CfDRevenues2020)/Year_difference;
for year = 1:4
Rev_CfD_2021to2024(year,:) = Aggregated_CfDRevenues2020 + year*Deltaperyear_2020_2025_CfD;
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!