MATLAB: How to construct a matrix with its column elements as matrix operations

discrete mathematicslinear algebraMATLABmatrixmatrix manipulation

Hello everyone,
I want to construct a M X N matrix with results imported from other matrices.
For example, I have 3 matrices
I want to construct a matrix with first column input as A+B, second column as B+C and third column as C+A
—-> Yellow column as A+B, Orange column as B+C, Blue column as C+A
Basically I want a matrix of all possible combinations. Please help me. Thank you.
Regards,
Azhar Uddin Mohammed

Best Answer

Assuming your column vectors are stored in cell array C:
combs = num2cell(nchoosek(1:numel(C), 2), 2);
S = cellfun(@(comd) C{comb(1)} + C{comb(2)}, combs, 'UniformOutput', false);
M = [S{:}];
Or, if your column vectors are actually columns of a matrix M1:
combs = num2cell(nchoosek(1:size(M1, 2), 2), 2);
S = cellfun(@(comb) sum(M1(:, comb), 2), combs, 'UniformOutput', false);
M2 = [S{:}];