sub2ind gives error: Error using sub2ind (line 73) Out of range subscript.
Show older comments
Im trying to adapt an existing script to match equivalent data but for a different time period.
The initial code is
Nfirms = 50000;
Tperiods = 1000;
Tshock = 800;
% pre-allocation;
sim_i_a = zeros(Nfirms,Tperiods);
sim_i_k = zeros(Nfirms,Tperiods);
sim_a_val = zeros(Nfirms,Tperiods);
sim_k_val = zeros(Nfirms,Tperiods);
sim_c_val = zeros(Nfirms,Tperiods);
sim_coll_val = zeros(Nfirms,Tperiods);
sim_effective_lambda_val = zeros(Nfirms,Tperiods);
sim_Psi_val = zeros(Nfirms,Tperiods);
sim_i_a(:,1) = index_a0;
sim_i_k(:,1) = index_k0;
sim_a_val(:,1) = a_grid(sim_i_a(:,1));
sim_k_val(:,1) = k_grid(sim_i_k(:,1));
sim_i_r = zeros(Tperiods,1);
sim_i_r(1:Tshock-1) = 6; %until 1993;
sim_i_r(Tshock:Tshock+17) =[5,... %1994
5,... %1995
5,... %1996
4,... %1997
3,... %1998
1,... %1999
2,... %2000
2,... %2001
1,... %2002
1,... %2003
1,... %2004
1,... %2005
2,... %2006
3,... %2007
2,... %2008
3,... %2009
2,... %2010
2]; %2011
sim_i_r(Tshock+18:end)=2;
And i changed sim_i_r to
sim_i_r = zeros(Tperiods,1);
sim_i_r(1:Tshock-1) = 2; %until 2011;
sim_i_r(Tshock:Tshock+6) =[2,... %2011
0,... %2012
0,... %2013
-1,... %2014
-1,... %2015
-1,... %2016
-1]; %2017
sim_i_r(Tshock+8:end)=-0.1;
But in doing so i get an error with the sub2ind function in the two following instances:
%generate paths;
it = 2;
index = sub2ind( size(sim_pol_ind_ap), sim_i_a(:,it-1),sim_i_k(:,it-1),sim_i_zp(:),sim_i_zt(:,it-1),sim_i_r(it-1)*ones(Nfirms,1),sim_i_tau(:,it-1),sim_i_q(:,it-1) );
for it = 2:Tperiods
sim_i_a(:,it) = sim_pol_ind_ap( index );
sim_i_k(:,it) = sim_pol_ind_kp( index );
index = sub2ind( size(c_val), sim_i_a(:,it),sim_i_k(:,it),sim_i_zp(:),sim_i_zt(:,it),sim_i_r(it)*ones(Nfirms,1),sim_i_tau(:,it),sim_i_q(:,it) );
sim_a_val(:,it) = a_grid(sim_i_a(:,it));
sim_k_val(:,it) = k_grid(sim_i_k(:,it));
sim_c_val(:,it) = c_val( index );
sim_coll_val(:,it) = binding_collateral( index );
sim_effective_lambda_val(:,it) = effective_lambda(index);
end
Sizes in the first argument of each sub2ind call, respectively,
size(sim_pol_ind_ap)
ans =
120 120 2 11 6
size(c_val)
ans =
120 120 2 11 6
I don't understand why i get this error, since i didn't change the size of the array sim_i_r, just the values it takes at different points.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!