Computing the centroid of some $n$ points in 3-dimensions Solving for the centroid of some set of points in 2-dimensions is trivial. I'd like, however, to determine the centroid in 3-dimensions, given an arbitrary set of $n$ points. How can this most easily be done?
Offhand, it seems that this should require some sort of integral. I'm not sure though.
Note that I speak of the centroid which minimizes the sum of the
squared distances.
 A: Just as the squared distance between two points in 2D space is of the form $x^2+y^2$, so is the squared distance between two points in $n$-D space of the form $x_1^2+x_2^2+\cdots+x_n^2$. Thus the centroid in any dimension of Euclidean space can be computed in exactly the same way as the two-dimensional centroid – average per-coordinate.
A: Given $S = \{y_1, y_2, y_3, ..., y_n\}$, you want to find a point $P$ to minimize $$\sum_{\boldsymbol x \in S}||P-\boldsymbol x||^2$$
Let $Y_k$ be the $k$th value of the point $Y$ (i.e. $Y_k$ would be the $x$ coordinate, $Y_2$ would be the $y$ coordinate, etc).
Then, the sum of the squared distances is
$$\sum_{\boldsymbol x \in S} \sum_{k=1}^d\left(P_k-\boldsymbol x_k\right)^2$$
Reversing the order of summation yields $$\sum_{k=1}^d\sum_{\boldsymbol x \in S} \left(P_k-\boldsymbol x_k\right)^2$$
From this, each coordinate can be minimized separately, so for $k = 1...d$, you need to find $P_k$ to minimize $\sum_{\boldsymbol x \in S} \left(P_k-\boldsymbol x_k\right)^2$. Taking the derivative with respect to $P[k]$ yields $$\sum_{\boldsymbol x \in S}\left(2P_k-2\boldsymbol x_k\right) = 0 \to P_k = \frac{\sum_{\boldsymbol x \in S}\boldsymbol x_k}{|S|}$$
This means that $P$ is given by the average of the points in $S$.
