adding a sequence of matrices
1 view (last 30 days)
Show older comments
How can I add G number of matrices? I mean I need to sum G number of matrices. For example: G=3, so, x1=[1 2 3 4; 2 3 4 5], x2=[1 1 2 2; 2 3 3 4], x3=[4 5 6 7; 1 2 3 4].
I need x1+x2+x3
It seems very simple, but if G varies, say, in every run of a simulation, I need something general, like x1+x2+.....+xG
Accepted Answer
the cyclist
on 19 May 2012
One way:
Use cell arrays to define your individual arrays.
x{1}=[1 2 3 4; 2 3 4 5];
x{2}=[1 1 2 2; 2 3 3 4];
x{3}=[4 5 6 7; 1 2 3 4].
% ...
x{G} = [1 2 1 2; 8 8 8 8];
Then sum them with a loop
Y = x{1};
for gi = 1:G
Y = Y + x{gi};
end
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!