While loop

2 次查看(过去 30 天)
Dougie
Dougie 2012-5-3
Hoping somebody could help me fix my while loop.
Whilst the output to a function Convergence == 0 i want the while loop to continue to iterate, producing an output matrix R_2, and also updating the run_number to run_number + 1. It's not necessary to store the previous matrices, if it's faster to run then the matrices could be replaced.
Here's what i have so far.
R_2 = %%not sure what goes here.
Conv = 0; %initially convergence criteria = 0
run_number = 1;
R_2{run_number} = R_2;
while Conv == 0
R_1 = %%%%function producing R_!;
Radius = %%function producing
R_2{run_number} = %function of R1 and Radius
Conv{run_number} = Conv(R_2{run_number});
run_number = run_number+1;
end
Thanks a lot

回答(1 个)

Walter Roberson
Walter Roberson 2012-5-3
Conv{1} = 0; %instead of Conv = 0;
while Conv{end} == 0
and use
Conv{run_number} = Conv{R_2{run_number}}; %instead of Conv()
However, please examine this statement more carefully:
Conv{run_number} = Conv{R_2{run_number}};
On the first iteration, Conv{1} is the only thing that exists, so if R_2{1} is not exactly 1 then you would be accessing out of bounds. So R_2{1} must be 1, and you are then setting Conv{1} = Conv{1} and that was initialized to 0. Second iteration, Conv{1} is still the only thing that exists, so R_2{2} must be 1, and you are setting Conv{2} = Conv{1} which is 0. Third iteration, the existing Conv{1}=0 and Conv{2}=0 are what exists, so Conv{3}=0. By induction, you are going to continue the loop with 0's until you run out of memory.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by