필터 지우기
필터 지우기

Loop Computation with i=1:N

조회 수: 22 (최근 30일)
Lorenzo Pinna
Lorenzo Pinna 2021년 2월 2일
댓글: Lorenzo Pinna 2021년 2월 2일
N=20000; %
sigma=6; %CES
w=1;
T=1;
rho=1/sigma;
f=0.75;
delta=0.02;
M=100000;
V=1;
%%
tic;
for i=1:N;
%Productivity draw
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng=sd;
Prod.av=sum(phi,2)./N; %Average Productivity
p_i(i)=((w)./(rho.*phi(i))); %Pricing rule
phi_tilda=(sum(phi(i)./Prod.av)).^sigma./((sum((phi(i)./Prod.av).^sigma))./(phi(i)));%Aggregate productivity = weighted harmonic mean
P=(N^(1/(1-sigma))).*(w/(rho*phi_tilda)); %D-S Price index
Q=(M.*V)./P;
q_i(i)=Q.*((p_i(i)./P).^(-sigma));
R=P.*Q;
r(i)=R.*((P.*rho.*phi(i)).^(sigma-1));
PI=N.*((R./sigma).'.*(P.'.*rho.*phi_tilda).^(sigma-1))-f;
pi(i)=r(i)./sigma-f;
end
Hello there!
I am inside of this loop. All seems working excepts from r(i) computation onwards. Albeit q_i(i) seems to be computed properly, r(i) presents a similar computation but, at the end of the day, the vector is computed as if i is equal to the 20000th observation ignoring all the others 19999. I do not know why matlab does not consider that the index i is composed by 20000 obs..
Indeed, if I compute r(1:N)=R.*((P.*rho.*phi(1:N)).^(sigma-1)); seems to work and it is what I want. How can I solve this? I.e: Obtain the same result of r(1:N) for r(i) as I did in q_i(i) and p_i(i).
Thanks, here below the script:
  댓글 수: 2
Daniel Pollard
Daniel Pollard 2021년 2월 2일
Please format your code correctly -
using Alt+enter -
with the appropriate indenting (ctrl+i is useful for that) to make it easier to read, and therefor easier to help you.
Lorenzo Pinna
Lorenzo Pinna 2021년 2월 2일
편집: Lorenzo Pinna 2021년 2월 2일
Done! Thanks for the hint.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2021년 2월 2일
What do you expect this code segment to do?
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng=sd;
If you expect this to call the rng function to store the random number state, generate some numbers, then reset the random number state it will not. The third line tells MATLAB that rng is a variable and so MATLAB cannot call the rng function in your code.
To do what you expect instead call rng on the first line rather than assigning to rng.
sd=rng;
phi(i)=normrnd(1,0.15); %G-distribution Draw
rng(sd);
But note if you do this, all the elements of phi will be the same.
In general, if you're resetting the random number generator inside a for loop you're probably doing something you shouldn't. It would be like a casino shuffling a deck of cards back to the same state each time a game starts -- the gamblers would probably pick up on that pretty quick. Initializing the generator before the loop and resetting it after the loop may be more appropriate.
  댓글 수: 1
Lorenzo Pinna
Lorenzo Pinna 2021년 2월 2일
Ok, thanks!
I changed this setting. However, the vector r(i) does not change. Everything seems to remain unchanged. Or better, have you find a solution to give r(i) exact as r(1:N)?
Thank you for the availability Steven

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by