MATLAB: “if it possible to only keep the value of the last two iterations”

matlab too slow

My simulation is running too slow because is keeping too many data. So, because I am using a double integrator, I would like to know if someone knows how to keep data from the last two iteration (i-1) and i in each loop. Thanks!

Best Answer

Seems fairly trivial, just store the results in a 1x2 cell array.
A=cell(1,2);
for i=1:10
A{1}=A{2};
A{2}=i
end
A{1} stores the previous results (i-1) and A{2} stores the current results, i.
Related Question