필터 지우기
필터 지우기

Need Help about this for loop

조회 수: 2 (최근 30일)
BN
BN 2020년 2월 5일
댓글: BN 2020년 2월 5일
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
Ajay Kumar
Ajay Kumar 2020년 2월 5일
편집: Ajay Kumar 2020년 2월 5일
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 2020년 2월 5일
Yor are using i for the outer loop and two inner loops. Maybe you shuld change index.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 2월 5일
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
Rik
Rik 2020년 2월 5일
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 2020년 2월 5일
Thank you, After doing that, it's worked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by