Calculating mean (Average) of vector

4.3k Views Asked by At

I'm trying to calculate PCA (Principle component analysis) and part of the equation is to calculate the mean of a vector $v$ and subtract each element of $v$ by it's mean. However, is this in column based, or, row based? I have the following:

$$V = \begin{pmatrix} 2.5 & 2.4 \\ 0.5&0.7 \end{pmatrix} = \begin{pmatrix} (2.5 + 2.4)/2 & \\ (0.5 + 0.7)/2& \end{pmatrix} = \begin{pmatrix} 2.45\\ 0.6 \end{pmatrix}$$

However, computing mean(V) in matlab, returns the following:

$$ V = \begin{pmatrix} 2.5 & 2.4 \\ 0.5&0.7 \end{pmatrix} = \begin{pmatrix} (2.5 + 0.5)/2 & \\ (2.4 + 0.7)/2& \end{pmatrix} = \begin{pmatrix} 1.5000\\ 1.5500 \end{pmatrix} $$

Also, in Matlab having the following:

$$V = \begin{pmatrix} 2.5 & 2.4 \\ 0.5&0.7 \\ 1.2&12 \end{pmatrix}$$

Returns 2 values for the mean, but, surely this should be 3?

Any help would be greatly appreciated!

2

There are 2 best solutions below

2
On

The function "mean" in MATLAB returns column wise mean for a matrix. PCA requires the mean for each column vector. In both of your Vs you have only two columns. It is not a surprise that matlab returns a 1-by-2 vector for both cases.

0
On

just do a matrix transpose.

mean(v') or mean(transpose(v)) will do the trick and get you the right answer.