change the iteration in for loop

조회 수: 25 (최근 30일)
NA
NA 2019년 7월 15일
댓글: Andy 2019년 7월 16일
I have For Loop that calculate c. Sometimes c become nan or big number.
When I get big amount or Nan for c, I want to repeat that iteration again.
I used this code but does not repeat that iteration.
for i=1:10
% calculate c
if isnan(c(i))==1 | c(i)>0.009
i=i-1 % again repeat the iteration
end
end
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 15일
Naime Comments:
I think '|' and '||' does not have a difference.
when it goes to if part, c become zero on that iteration.
it means that does not repeat that iteration.
for example in i=2, c(2)=nan. when I use this code i=1, but in for loop it starts from 3.
Big number in my case more than 0.009
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 15일
Naime Comments:
m=m-1; % matlab gives me this detail
it appears that the index value of the indicated for loop changes inside the loop body. Often, this happens when loops nest and an inner loop reuses the name of an outer loop index. Because MATLAB resets the loop index to the next value when it returns to the top of the outer loop, it ignores any changes that took place within a nested loop. If the code does not to refer to the outer loop index value after the inner loop changes it, no problem results. However if you attempt to use the outer loop index value after the inner loop completes, it is likely to result in a bug.

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

답변 (2개)

Andy
Andy 2019년 7월 15일
I don't think the For loop is what you need.
i =1;
while i<11
% calculate c
if isnan(c(i))==0 & c(i)<=0.009
i=i+1;
end
end
  댓글 수: 2
NA
NA 2019년 7월 15일
If i=5; nan happens,
i changes to 6, but c(5) is still nan.
I think i=i+1; does not work
Andy
Andy 2019년 7월 16일
I made up this code, adding the else just to change the value and it works fine.
i=1;
c=[ .001 12 .003 .004 nan .006 .007 .008 nan .005];
while i<11
%calculate c
if isnan(c(i))==0 & c(i)<=.009
i=i+1;
else
c(i)
c(i)=0;
end
end
c

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


KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 15일
편집: KALYAN ACHARJYA 2019년 7월 15일
# Experts need your suggestions here
for i=1:10
c(i)=...% do
while isnan(c(i)) || c(i)>0.009
c(i)=..
end
end
I know multiple loop is messed here, just try to get way out. Hope I undestand the question.
One Note: Without changing i, is there any possibilty to change the C(i) in next or next iterartions within while loop, so that once it fail, it exit from while loop?
The code runs within the while loop, without changing i ultill C(i) satisfy any one conditions-
  1. C(i) is NaN
  2. C(i)>0.009
  댓글 수: 2
NA
NA 2019년 7월 15일
for i=1:10
c(i)=...% do
while isnan(c(i)) || c(i)>0.009
c(i)=[]; % ignore calculated c(i)
end
end
result of c(i)
0.0038 0.0036 0.0029 0 0.0019 0.0011 0.0018
0.0011 0.0305
when i=4, nan happens so I want to repeat i=4 to get some result.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 15일
편집: KALYAN ACHARJYA 2019년 7월 15일
when i=4, nan happens so I want to repeat i=4 to get some result
When NaN appears, it enter to within while loop, while loop doesnot change the i value. When c(i)=Nan at i=4, it keep running withing while loop, that why I asking, is there any possibility to change C(i) result without changing i, so that it exit from while loop?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by