To stop the while loop you need to make sure that none of the conditions are met, else it will keep running. By changing || to && you are saying that both conditions need to be met in order to continue looping.
term_while = true;
c = 0 ;
vol = zeros() ;
while c >= 0 && term_while
...
if VolumeOfSilo < vol(c)
term_while = false;
end
end
Alternatively, you can use your orginal code but replace break with c = -1. This will prevent the condition for continuing the while-loop from being met.
c = 0 ;
vol = zeros() ;
while c >= 0
c = c + 1;
...
if VolumeOfSilo < vol(c)
c = -1;
end
end
However, looking over the intention of your function, you might consider using the following instead.
function [FullContainers,RemainingLiquid] = myFunction(VolumeOfSilo)
i = 0;
vol = 0;
while sum(vol)<=VolumeOfSilo && i<1e5
i = i+1;
vol(i) = 280+150*rand ;
end
FullContainers = numel(vol)-1;
RemainingLiquid = VolumeOfSilo-sum(vol(1:end-1));
end
0 Comments
Sign in to comment.