0
$\begingroup$

I have the following definitions

long  = 1000;
  init  = 500;      
  N = init+long;        
  T = init+1:N-1;   
  T1    = init+2:N;   

I then fill the vectors that I want according to this loop,

   for t=1:N+1
     Rk(t)=1+A(t)*alpha*kh(t).^(alpha-1)-delta;  
     Yi(t)=A(t)*(1-alpha)*kh(t).^alpha - A(t)*alpha*kh(t).^(alpha-1)+eta(t);
     Rap(t)=Rk(t).^(1-gamma)+(1-gamma)*Rk(t).^(-gamma)*theta(t)*Yi(t)-0.5*gamma*(1-gamma)*(theta(t)*Yi(t)).^2;  
   end   

Finally, I compute some new vectors according to this loop

for t=T1
           fi1(t)=(v(t).^(1-gamma))*(Rk(t))*(Yi(t)');  % Expectations 1 
           fi2(t)=((v(t).^(1-gamma))*(Yi(t)).^2);   
           fi3(t)=(((v(t).^(1-gamma))*Rap(t)).^(1/(1-gamma)));
       end

So, from the last loop I aim into getting a vector of size T1, but instead I receive a 1x1500 vector.

My scope is essentially to take all the numbers in vectors, v, Rk, Yi, from exactly the region that T1 specifies (i.e 502:1500), so I should get the 1x999, and not the 1x1500 vector.

So I suppose even if someone stars with an index 502 to count the loop, Matlab still understand this as 1, and loops until N, which is the end point of T1.

Can somebody tell me how should amend the code in order to get what I described above.

$\endgroup$

1 Answer 1

3
$\begingroup$

t starts at 502, but all MATLAB arrays start at 1. So MATLAB pads fi1, fi2, and fi2, with zeros from index 1 to 501 and then puts your data in starting at 502. You can either live with this or write:

for t=T1
  fi1(t-(init+1))=(v(t).^(1-gamma))*(Rk(t))*(Yi(t)');
  ......
end
$\endgroup$
1
  • 2
    $\begingroup$ Or, if you're not going to preallocate the array anyway, use the nice MATLAB syntax to append new elements to an array: fi1=[]; ...; for t=T1, fi1(end+1)=...; ...; end. $\endgroup$ Commented Feb 12, 2015 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.