For loop with Else statement

39 次查看(过去 30 天)
Hi all! I am running a loop of the form:
RR=flip(1:100);
CC=0;
threshold=3000;
for i=1:100
if CC<threshold
C_old=CC;
CC=sum(RR(1:i));
else
CC=C_old;
RR(i)=0;
end
end
The problem is when the loop passes through the else statement it automatically increase "i" by 1. Which leads to skipping values of the vector "RR". How can I fix this?
  2 个评论
Guillaume
Guillaume 2018-10-10
it automatically increase "i" by 1
It certainly doesn't so if that really happens it's because you have written code that explicitly does it.
We would need to see the actual code for us to tell you what is happening.
Miroslav Mitev
Miroslav Mitev 2018-10-11
Hi @Guillaume, I've added the code.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2018-10-11
Your code is behaving exactly as expected. It may not do what you want but that's because you've made a mistake.
The best way for you to understand what is happening is to use the debugger to step through your code one line at a time and see how the variables evolve.
  • At i = 37, CC is 2970, so the if is true, C_old is set to 2970 and CC becomes 3034.
  • At i = 38, the if is now false, hence CC gets assigned C_old so is set back to 2970. RR(38) is set to 0
  • At i = 39, since CC is now again 2970 the if is true, C_old is set again to 2970 (the same value it already had), CC becomes 3220
  • At i = 40, the if is now false, hence CC gets assigned C_old which is still 2970. RR(40) is set to 0.
  • At i = 41, see step 39
  • and so on ... every even i set RR(i) to 0, every odd i, CC is 2970.
I have actually no idea what your code is trying to achieve. My feeling is that you probably don't need a loop.
  2 个评论
Miroslav Mitev
Miroslav Mitev 2018-10-11
Thanks. I fixed the issue by using another if loop instead of else statement. It works fine now:
RR=flip(1:100);
CC=0;
threshold=3000;
for i=1:100
if CC<threshold
C_old=CC;
CC=sum(RR(1:i));
end
if CC>threshold
CC=C_old;
RR(i)=0;
end
end
Guillaume
Guillaume 2018-10-11
As said, a loop is not necessary:
RR = flip(1:100);
CC = cumsum(RR);
RR(CC > threshold & CC(find(CC < threshold, 1, 'last')) + RR > threshold) = 0;
Note that you may want to change either the > or the < into >= or <= respectively.

请先登录,再进行评论。

更多回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-10-10
编辑:KALYAN ACHARJYA 2018-10-10
for i=1:100
if b(i)>1
do something
else
do something else
end
end
No, this cant, except "do something else" statement include i=i+1, other any other i increment statement.
For Example
b=-5:1:100
c=1;
for i=1:100
if b(i)>1
disp(b(i));
else
c=c+1;
end
end
  2 个评论
Miroslav Mitev
Miroslav Mitev 2018-10-11
Hi, @KALYAN, please see the code in the question above.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-10-11
编辑:KALYAN ACHARJYA 2018-10-11
@Miroslavi value is not skipped (I checked it by disp(i))
RR=flip(1:100);
CC=0;
threshold=3000;
for i=1:100
if CC<threshold
disp(i);
C_old=CC;
CC=sum(RR(1:i));
else
disp(i);
CC=C_old;
RR(i)=0;
end
end

请先登录,再进行评论。


Dennis
Dennis 2018-10-11
My guess is that you want to set every value in RR to 0 after the cumulative sum reaches 3000.
Here is why this does not work:
CC is the sum of RR(1:i), once CC reaches 3001 you enter your else statement. In your else statement:
CC=C_old; % you set CC to zero
RR(i)=0;
In the next iteration of your loop CC will initially be 0. Hence it enters your if statement:
CC=sum(RR(1:i)); %this will be >3001
So basically from here on your loop will alternate between if and else.
Now i am not completly sure what you want to do, if my assumption was correct that you want to set values in RR to 0 after the sum reaches a specific value you can try this code:
RR=100:-1:1;
A=cumsum(RR);
RR(A>3000)=0;

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by