how to sum (or recall) matrices from previous cycles?

hi everyone,
I've got a "for" cycle of i=1:100 iterations, and in every iteration MATLAB calculates a matrix called "A".
Now for every iteration I need to sum the current matrix "A" with the previous same five matrices.
If "A" was a scalar I could do:
a(i)=a(i-1)+a(i-2)+a(i-3)+a(i-4)
but I don't now how to handle this with a matrix...could you help me?
thanks a lot!! fab

 採用された回答

David Young
David Young 2012 年 2 月 26 日

0 投票

You can store the matrices using a cell array:
for i = 1:100
< compute A >
Astore{i} = A;
if i >= 5
arraySum = A{i-1} + A{i-2} + A{i-3} + A{i-4};
< use arraySum >
end
end
You can also do this with a multidimensional array, and if the matrices are large, you can avoid storing all 100 of them by only keeping the last 5. Please say if you want more details of either of these options.

2 件のコメント

Faber
Faber 2012 年 2 月 26 日
David thank you for your answer,
it seems to me that there is always a problem, using:
Astore{i} = A;
MATLAB tells me " In an assignment A(I) = B, the number of elements in B and I must be the same."
because I think Astore looks like a scalar in this way...have I forgot something?
thank you!
Faber
Faber 2012 年 2 月 26 日
sorry David, it's my fault, I didn't noticed the {} brackets, now everything works! Thank you very much for your help!
Bye
Fab

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2012 年 2 月 26 日

編集済み:

2013 年 10 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by