What is wrong with this while loop
3 views (last 30 days)
Show older comments
I have written a long-ish function, it says "busy", heats up the computer, and does not finish even if I reduce the size of the arrays involved drastically. When I ctrl+C it, it always terminates on a while statement, or the if statement nested inside it, or the "end" for those 2.
This is the code, with the while loop and if statement written in full:
for t=2:(totaltime/timestep)
<stuff, including arraysize(t)=arraysize(t-1)>
for i=1:arraysize(t-1)
<lots of stuff, including increasing arraysize(t) - I thought the issues would crop up here>
end
n=(0.1/60)*timestep*currentcells(t-1);
while remcells(t)<n
r=randi(arraysize(t)); %line 108
if yeast.biomass(t,r)>0 %line 109; yeast.biomass is an array of size arraysize(t), I'm picking n random elements from it
remcells(t)=remcells(t)+1;
currentcells(t)=currentcells(t)-1; % I thought it was a potential problem but removing it made no difference
<some undemanding stuff>
else
continue
end
end
<very undemanding stuff>
end
Given that whenever I terminate it, it ends at that same line
"Operation terminated by user during timevolution (line 108)"
(More usually) "Operation terminated by user during timevolution (line 109)"
I assume there's something wrong with that while loop or the if statement. Is there? Or does it ending at that same line not mean anything?
Any help will be greatly appreciated! I am a novice at Matlab (and coding in general) so if there's something "obvious" that might be a solution please point it out...
2 Comments
Walter Roberson
on 20 Oct 2015
What evidence is there that yeast.biomass(t,r)>0 for any r ? Or that n is not Very Big ?
Answers (1)
Star Strider
on 20 Oct 2015
I can’t run your code, so these are wild guesses. There are two possibilities that I see:
First, see what the end condition for the for loop is, specifically the value of (totaltime/timestep). The default increment is 1 for colon-operator vectors. The end condition value could be larger than you thought.
Second, check the value of ‘yeast.biomass(t,r)’ is to be certain you enter the if block. Write the values of ‘n’ and ‘remcells(t)’ to the Command Window within your while loop. The while condition might never be satisfied.
These are where I would look.
2 Comments
Star Strider
on 21 Oct 2015
It’s best not to take anything for granted. Check everything — the values of all variables — that might be a part of this problem.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!