sub2ind gives error: Error using sub2ind (line 73) Out of range subscript.

1 view (last 30 days)
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

Sindar
Sindar on 24 Sep 2020
sim_i_r was valid indices (integers greater than zero), but you changed it to invalid ones (zeros, decimals, negatives). Matlab should give a better warning ("invalid index" vs "index out of range")
  4 Comments
Sindar
Sindar on 24 Sep 2020
if they don't get updated at any point, then sim_i_a & sim_i_k are being set to zero at every iteration, and then passed to sub2ind
If they are getting updated, please include those lines but also let's try debugging. Run this line before starting the code:
dbstop if error
this will automatically enter debugging mode when the first error is thrown. Then, you can look at the values actually being passed to sub2ind, and check whether they are within the 1-size range you expect

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!