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

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.

 Respuesta aceptada

Sindar
Sindar el 24 de Sept. de 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 comentarios

Rui Santos
Rui Santos el 24 de Sept. de 2020
Editada: Rui Santos el 24 de Sept. de 2020
Yes, however,
when I changed it to, for instance
sim_i_r(1:Tshock-1) = 2; %until 2011;
sim_i_r(Tshock:Tshock+6) =[2,... %2011
1,... %2012
2,... %2013
1,... %2014
3,... %2015
4,... %2016
1]; %2017
sim_i_r(Tshock+8:end)=1;
I no longer get the error of the sub2ind function outside the loop but now i get a subscript out of range error in the sub2ind function inside the for loop
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
where/how do you define sim_pol_ind_ap & sim_pol_ind_kp?
They're defined as:
nzt = 11;
nq = 1 ;
ntau = 1;
nr = 6;
na = 120 ;
nk = 120 ;
nzp = 2 ;
sim_pol_ind_ap = zeros(na,nk,nzp,nzt,nr,ntau,nq);
sim_pol_ind_kp = zeros(na,nk,nzp,nzt,nr,ntau,nq);
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Sept. de 2020

Comentada:

el 24 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by