A have a data se of 1000 normally distributed values representing annual discount rate (dr). I wrote a code that randomly picks 20 values from the data set and assigns it to a formula that calculates net present value (npv) using those 20 values. What I want it to do is to run the simulation n-times and save the value of npv in a vector npvsim after each simulation. Numper of simulations is defined by u=length(npvsim), in this case 5.
What I get in results at the moment is the same value stored five times. It seems to me that i=randsample(dr,20) takes a sample once and the same values are then used for 5 times. Is there any way I can make these 20 values to be refreshed for each iterations? I'm kind of stuck and the existing answers provite little help.
Thank you in advance.
npvsim = zeros(5,1);
for u = length(npvsim) ;
%selecting 20 random discount rates for each year
i = randsample(dr,20);
for n = reshape(1:20,20,1);
npv = -2000+ sum(571.5./((1+i).^n));
npvsim(:,u') = npv;
end;
end;

 採用された回答

Walter Roberson
Walter Roberson 2022 年 2 月 23 日

0 投票

for u = 1:length(npvsim)

1 件のコメント

Evgeny Nikolaev
Evgeny Nikolaev 2022 年 2 月 25 日
It works and makes perfect sence, thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by