필터 지우기
필터 지우기

How to save the last four iterations in a for-loop

조회 수: 1 (최근 30일)
KostasK
KostasK 2022년 3월 21일
답변: Voss 2022년 3월 21일
Hi all,
I have a basic for-loop and I would like to know how I can only save (or retain) the last 4 iterations of the loop instead of all. For example:
clear ; clc
b = 2 ;
for i = 1:100
a(i) = b + 4 ;
end
would save all iterations from i=1 to 100. How could I have only the last four while the loop is running? (that is without having the for-loop execute and then just extract the final four iterations)
Thanks for your help in advance.

채택된 답변

Matt J
Matt J 2022년 3월 21일
편집: Matt J 2022년 3월 21일
One way:
for i = 1:100
a(max(i-96,1)) = b + 4 ;
end

추가 답변 (1개)

Voss
Voss 2022년 3월 21일
Here's one way:
clear ; clc
b = 2 ;
for i = 1:100
if i > 96
a(i-96) = b + 4 ;
end
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by