Need Help about this for loop

1 visualización (últimos 30 días)
BN
BN el 5 de Feb. de 2020
Comentada: BN el 5 de Feb. de 2020
Hey all, I have a very simple issue about saving for loop results in the one variable at the end.
I have this code. In the First for loop which is for i =1:6. If you looking at output part (at the end of the code) you can see: SI(sc:end,1)=SI1; I want all sc (for i=1:6) outputs save in the SI (multiple columns). At this time only the results of i=6 saves in the SI.
sc_vect=[1 3 6 12 24 48];
for i=1:6
sc = sc_vect(i);
% sc: scale of the index (>1, e.g., 3-month SPI or SSI)
n=length(td);
SI=zeros(n,12);
% Compute the SPI for each grid from the prcp or smc data
%For some grid, no observation exist.
if length(td(td>=0))/length(td)~=1
SI(n,1)=nan;
else
% Obtain the prcp and smc for the specified time scale and
% compute the standarized drought index (for SPI and SSI)
SI(1:sc-1,1)=nan;
A1=[];
for i=1:sc,
A1=[A1,td(i:length(td)-sc+i)];
end
Y=sum(A1,2);
% Compute the SPI or SSI
nn=length(Y);
SI1=zeros(nn,1);
for k=1:12
d=Y(k:12:nn);
%compute the empirical probability
nnn=length(d);
bp=zeros(nnn,1);
for i=1:nnn
bp(i,1)=sum(d(:,1)<=d(i,1));
end
y=(bp-0.44)./(nnn+0.12);
SI1(k:12:nn,1)=y;
end
SI1(:,1)=norminv(SI1(:,1));
%output
SI(sc:end,1)=SI1;
end
end
  4 comentarios
Ajay Kumar
Ajay Kumar el 5 de Feb. de 2020
Editada: Ajay Kumar el 5 de Feb. de 2020
Rik meant to use debugger to go line by line. Not to run whole code at once.
Put a breakpoint on line 1 and go line by line to see how the variables are storing.
Marcel Kreuzberg
Marcel Kreuzberg el 5 de Feb. de 2020
Yor are using i for the outer loop and two inner loops. Maybe you shuld change index.

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 5 de Feb. de 2020
You are reusing i as a loop index multiple times. The mlint also warns you about that. It is a good idea to resolve all warnings mlint gives you, or mark them with the ok pragma (right-click on the orange underlined text and select 'on this line' in the suppress warning menu). That way you can instantly see if an edit caused a new warning.
As for the issue you are describing: the line below resets SI every time the outer loop runs.
SI=zeros(n,12);
You also don't assign any values to the later columns of SI. I suspect you want to have something like this in your code:
SI(sc:end,i)=SI1;
%(assuming you keep using i as a variable and use it as your loop index for the outer loop)
Although it is common in many programming languages, it is generally recommended to avoid i and j as variable names to avoid confusion or bugs caused by interpretation as the imaginary unit.
  4 comentarios
Rik
Rik el 5 de Feb. de 2020
You keep clearing your output:
SI=zeros(n,1);
You should do something like this:
td=Abadan.rrr24;
Date = Abadan.date;
sc_vect=[1 3 6 12 24 48];
n=length(td);
SI=zeros(n,numel(sc_vect));
for p=1:numel(sc_vect)
sc = sc_vect(p);
BN
BN el 5 de Feb. de 2020
Thank you, After doing that, it's worked.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by