Clear Filters
Clear Filters

Using while loop

1 view (last 30 days)
Andy McCann
Andy McCann on 11 Apr 2012
I am performing a series of calculations on a matrix in a while loop. The output matrix from the series of calculations is what determines whether the next cycle takes place or not. But then how do i make the input to the calculations stage be the output of the previous iteration or is this what matlab performs in the while loop anyway? Thanks

Answers (2)

Sean de Wolski
Sean de Wolski on 11 Apr 2012
That's what it does anyway. Maybe this will help explain:
M = magic(5)
while ~isempty(M)
M(1,:) = []
end

Sargondjani
Sargondjani on 11 Apr 2012
actually this is very simple. if you do some calculations in your while looop, and assign a value to some variable, then this variable will keep this value in the next cycle... until you overwrite it. if you want to do calculations with this variable than you will have the initialize it before the while loop;
OUTPUT=OUTPUT0; %initialization
while %your statement
%calculations, probably involving variable 'OUTPUT'
OUTPUT = % the number you want it to be.
end
of course, you can also store the value of OUTPUT in a vector, or matrix, array or whatever so you can track it afterwards. This would look like this if OUTPUT has to contain an array (replace {} with () if OUTPUT has just 1 value):
iter =1;
OUTPUT{iter}=OUTPUT0;
while
%calculations with OUTPUT{iter}
iter=iter+1;
OUTPUT{iter}=
end
I hope this answers your question...

Community Treasure Hunt

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

Start Hunting!