MATLAB: Summation of each pair of columns in a matrix

matrix manipulation

Hi
suppose that matrix A is:
A=[1 2 3;2 1 1;1 2 2]
I wan to obtain the summation of each pair of columns and show the result in matrix R such as:
R=[8 9 10; 9 10 11;10 11 12]
where Ri,,j determines the summation of elements of i-th and j-th columns of matrix A
(my data is big and I dont want to use any loop)
Thank you

Best Answer

Most likely, given the limited explanation:
sum(permute(A, [3 2 1]) + permute(A, [2 3 1]), 3)
The following may be slightly more efficient for large matrices (only one permute):
squeeze(sum(A + permute(A, [1 3 2]), 1))