フィルターのクリア

How to save only latest ~10 iterations on for loop

4 ビュー (過去 30 日間)
sasha
sasha 2014 年 6 月 12 日
コメント済み: Geoff Hayes 2014 年 6 月 12 日
I'm trying to run a for loop that will give me a new iteration of a matrix at each time step. The problem comes in because I need to calculate up to 20,000 time steps, but I don't have any where near the memory for that. I'm only interesting in the last ~ 10 time steps. Is there some way to write over or erase the older time steps?

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 6 月 12 日
編集済み: Geoff Hayes 2014 年 6 月 12 日
If you are only interested in the latest 10 matrices, then you can create a "circular" cell array of fixed size that is updated at each iteration, overwriting the older matrices as the code iterates:
circularArraySz = 10;
circularArray = cell(circularArraySz,1); % create the circular cell array
nextIdx = 1; % the index into the array in which
% to insert the next matrix
% do the for loop
for i=1:20000
% do the work to generate the matrix A
% add A to the cell array
circularArray{nextIdx} = A;
% increment to the next index
nextIdx = nextIdx + 1;
% wrap around to the beginning of the circular array if the index
% is greater than circularArraySz
if nextIdx>circularArraySz
nextIdx = 1;
end
end
When the loop ends, circularArray will have the last ten matrices. Try it out and see what happens!
  6 件のコメント
sasha
sasha 2014 年 6 月 12 日
Thanks! This works for my test functions!
Geoff Hayes
Geoff Hayes 2014 年 6 月 12 日
Awesome!

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 12 日
look at this example, I'm not sur if it's the best way
a=[];
for k=1:20
a(end+1)=k
a(1:end-10)=[]
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by