Calculating and and recording a vector during each iteration of a "while" loop.

4 次查看(过去 30 天)
I have searched the forums for similar answered questions and tried the solutions, but I unfortunately I can't get any of them to work for me.
(...)
lambda = 1;
lambda0 = 2;
while abs(lambda0 - lambda) > (1*10^-6)
R = M * R;
R = R / norm(R,1);
lambda0 = lambda;
lambda = R' * M * R / (R' * R);
end
Rfinal = R;
I've performed a while loop during which, through iterations, I reach a final set of values saved as vector "R". The final vector "R" is saved as "Rfinal".
Afterwards, in each iteration of that previous while loop, I need to record the values of as "power_err" using the "R" of the current iteration. This will allow me to plot log10(power_err) for the iterations.
I'm unable to write a proper code to fulfil that task. Any help is appreciated.

采纳的回答

James Tursa
James Tursa 2021-4-7
编辑:James Tursa 2021-4-7
E.g., using a counter to store all of your iteration data in a cell array and then post process this:
k = 1; % the counter
Rcell = cell(1,1000); % some size larger than you expect
Rcell{1} = R;
while abs(lambda0 - lambda) > (1*10^-6)
R = M * R;
R = R / norm(R,1);
lambda0 = lambda;
lambda = R' * M * R / (R' * R);
k = k + 1;
Rcell{k} = R; % store current R in cell array
end
Rfinal = R;
power_err = zeros(1,k);
for x=1:k
power_err(x) = your calculation here involving Rfinal and Rcell{x}
end

更多回答(0 个)

类别

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

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by