Need Help about this for loop
Show older comments
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 Comments
Rik
on 5 Feb 2020
Have you tried using the debugger to step through the code line by line?
BN
on 5 Feb 2020
Ajay Kumar
on 5 Feb 2020
Edited: Ajay Kumar
on 5 Feb 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
on 5 Feb 2020
Yor are using i for the outer loop and two inner loops. Maybe you shuld change index.
Accepted Answer
More Answers (0)
Categories
Find more on File Operations 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!