Using while loop
이전 댓글 표시
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
답변 (2개)
Sean de Wolski
2012년 4월 11일
That's what it does anyway. Maybe this will help explain:
M = magic(5)
while ~isempty(M)
M(1,:) = []
end
Sargondjani
2012년 4월 11일
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...
카테고리
도움말 센터 및 File Exchange에서 Utilities for the Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!