Need Help about this for loop

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

Have you tried using the debugger to step through the code line by line?
Dear Rik,
I didn't get any error. the code running successfully.
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.
Yor are using i for the outer loop and two inner loops. Maybe you shuld change index.

Sign in to comment.

 Accepted Answer

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

BN
BN on 5 Feb 2020
Edited: BN on 5 Feb 2020
Dear Rik
Thank you for let me know. According to your answer I changed my code to this:
td=Abadan.rrr24;
Date = Abadan.date;
sc_vect=[1 3 6 12 24 48];
for p=1:6
sc = sc_vect(p);
n=length(td);
SI=zeros(n,1);
% 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,p)=SI1;
end
end
But still, after running, the results of i=6 saves in the sixth column of SI and first to the fifth column stay zero.
Do you know what is the problem? I attach my data for you named is Abadan.
Thank you
I think this problem cant solve by matlab, it's very complicated for me so een I don't like I must using this code in order to achive SI3 and SI6 and so on without loop. If anyone can help me in this regard to how make a loop and summerize this code I would be so greatful.
td = Abadan.rrr24;
Date = Abadan.date;
% sc: scale of the index (>1, e.g., 3-month SPI or SSI)
sc=3;
n=length(td);
SI=zeros(n,1);
% 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
%plot vector
f=figure(1);clf(f)
plot(Date,SI, 'linewidth',1.5)
xlabel('time step')
ylabel('Standardized Precipitation Index 1')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%SPI 6month
%% main code
% sample input
sc=6;
n=length(td);
SI6=zeros(n,1);
% 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
SI6(n,1)=nan;
else
% Obtain the prcp and smc for the specified time scale and
% compute the standarized drought index (for SPI and SSI)
SI6(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
SI6(sc:end,1)=SI1;
end
%plot vector
f=figure(1);clf(f)
plot(Date,SI6, 'linewidth',1.5)
xlabel('time step')
ylabel('Standardized Precipitation Index 6')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% SAME PROCESS FOR ALL 6 IS THAT I NEED
%% THEN I CAN USE FIRST COLUMN OF EACH SI TO MAKE A INTEGRATED 6 COLUMNS TABLE
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);
Thank you, After doing that, it's worked.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Asked:

BN
on 5 Feb 2020

Commented:

BN
on 5 Feb 2020

Community Treasure Hunt

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

Start Hunting!