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

조회 수: 22 (최근 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