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

3 ビュー (過去 30 日間)
Mak Dukan
Mak Dukan 2021 年 10 月 5 日
コメント済み: Mak Dukan 2021 年 10 月 5 日
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 :)

採用された回答

Nathan Bblanc
Nathan Bblanc 2021 年 10 月 5 日
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 件のコメント
Kevin Holly
Kevin Holly 2021 年 10 月 5 日
編集済み: Kevin Holly 2021 年 10 月 5 日
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
Mak Dukan
Mak Dukan 2021 年 10 月 5 日
This was really helpful. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by