MATLAB: All the combinations of sums of rows of matrices

adding rows of matricessums of rows

Hello, I looked for this problem but I couldn't find any convenient solution. Assume that I have k matrices whose dimensions are m(1)xn, m(2)xn, … m(k)xn. I need all the possible sums of rows of these k matrices.
For example, let us have 3 different matrices.
A =
1 0 2 1
2 4 1 3
0 1 3 2
B =
1 3 0 5
C =
2 1 3 0
1 4 0 3
Then the result should be the following.
Result =
4 4 5 6 ( A(1,:) + B(1,:) + C(1,:) )
3 7 2 8 ( A(1,:) + B(1,:) + C(2,:) )
5 8 4 8 ( A(2,:) + B(1,:) + C(1,:) )
4 11 1 10 ( A(2,:) + B(1,:) + C(2,:) )
3 5 6 7 ( A(3,:) + B(1,:) + C(1,:) )
2 8 3 9 ( A(3,:) + B(1,:) + C(2,:) )
I hope that I could explain my problem. If there is an ambigous part, please ask me to fix your confusion.
Thanks for your efforts.

Best Answer

Code for arbitrary number of matrix
A=[...
1 0 2 1
2 4 1 3
0 1 3 2];
B=[...
1 3 0 5];
C=[...
2 1 3 0
1 4 0 3];
% Start from here
CA = {A,B,C}; % add more matrices if needed
CA = CA(:);
s1 = cellfun('size',CA,1);
c = arrayfun(@(n) 1:n, s1, 'unif', 0);
[c{:}] = ndgrid(c{:});
n = length(CA);
c = reshape(cat(n+1,c{:}),[],n)';
c = c + cumsum([0;s1(1:n-1)]);
AA = cell2mat(CA);
R = reshape(AA(c,:),n,[],size(A,2));
R = reshape(sum(R,1),[],size(A,2))