Need Help about this for loop

1 view (last 30 days)
BN
BN on 5 Feb 2020
Commented: BN on 5 Feb 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 Comments
Ajay Kumar
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
Marcel Kreuzberg on 5 Feb 2020
Yor are using i for the outer loop and two inner loops. Maybe you shuld change index.

Sign in to comment.

Accepted Answer

Rik
Rik on 5 Feb 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 Comments
Rik
Rik on 5 Feb 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 on 5 Feb 2020
Thank you, After doing that, it's worked.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!