How can I store and sum matrix using loop for?

Hi threre.
I have the following matrices and the code I am running to get the sum of matrices:
A = sparse matrix (20000x20000)
I = matrix identity of A
for ii = 1:200
C{ii} = A^ii;
L = I + C{ii};
end
What I would like to get as result is L = I+C{1}+C{2}+C{3}...+C{200}.
However I am getting L = I+C{200}.
Could you help me to find the mistake, please?

 Accepted Answer

Matt J
Matt J on 26 Mar 2022
Edited: Matt J on 26 Mar 2022
A = sparse matrix (20000x20000)
L = speye(size(A));
for ii = 1:200
C{ii} = A^ii;
L = L + C{ii};
end

3 Comments

I see.
should set a L matrix before run the code.
Thanks a lot dear Matt.
Cheers
The main change was
L = L + C{ii};
Yes, I got you.
Many thanks.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!