save n-Numbers of last interation from a loop

조회 수: 1 (최근 30일)
Jan Krumpak
Jan Krumpak 2018년 11월 28일
답변: Jan 2018년 11월 28일
hello
i want to save always the last n (n between 1 and 6) interation of a loop.
i have a regulator which is read out in a loop. now i need always the last n inerations of the loop to calculate the desirerd condition.
function ReglerButtonPushed(app, event)
k= app.KWertSlider.Value;
bereich= app.BereichSlider.Value;
speed= app.SpeedSlider.Value;
fehlervek= zeros(6,1);
l
app.stopRegler = false;
while app.stopRegler == false
b = getBrightness(app,app.sensorPos(1),app.sensorPos(2));
app.HelligkeitEditField.Value=b;
fehler = b - 0.5; %actual mistake
i know how to save all mistakes - i just need the actually last n interations.

채택된 답변

Jan
Jan 2018년 11월 28일
n = 6;
fehlerVec = nan(1, n);
while app.stopRegler == false
b = getBrightness(app,app.sensorPos(1),app.sensorPos(2));
app.HelligkeitEditField.Value=b;
fehler = b - 0.5; %actual mistake
fehlerVec = [fehler(2:n), fehler];
end
Now fehlerVec contains the last n elements. There are more efficient solutions, but collecting these n values is most likely not the time-crticial part.

추가 답변 (0개)

카테고리

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