2.65 How to add the mean of each column of a matrix from each column?

Given \[ \left ( {\begin {array} {cc} 1 & 2 \\ 3 & 4 \end {array}} \right ) \] Find the mean of each column, and add this mean to each element of the corresponding column. The result should be

\[ \left ( {\begin {array} {cc} 3 & 5 \\ 5 & 7 \end {array}} \right ) \]

To subtract the mean, just replace Plus with Subtract below for Mathematica and replace @plus with @minus for Matlab. This shows that Matlab bsxfun is analogue to Mathematica’s MapThread.

Mathematica

mat = {{1, 2}, {3, 4}} 
MapThread[Plus,{Transpose@mat, Mean[mat]}]
 

    {{3, 5}, {5, 7}}
 

 

Matlab

A=[1 2;3 4]; 
bsxfun(@plus, A, mean(A))
 

ans = 
     3     5 
     5     7
 

 

Maple

Maple mean always return real value, even if exact is possible.

A:=Matrix([[1,2],[3,4]]); 
for n from 1 to LinearAlgebra:-ColumnDimension(A) do 
    A[..,n]:=A[..,n] +~ Statistics:-Mean(A[..,n]) 
od; 
A
 

\[ \left [\begin {array}{cc} 3.0 & 5.0 \\ 5.0 & 7.0 \end {array}\right ] \]