How does differentiation work on matrices? I am studying on K-Means clustering. I am wondering how the differenciation work on the matrices circled in red below in the image. 
Can I have your expertise on how the calculations done in the circles below?
I am sorry if I have tagged my question in the wrong places.
Thank you.

 A: Suppose
$$ x=(x_1,x_2,...,x_n)^T $$
and
$$ y = (y_1,y_2,...,y_n)^T $$
then
$$ x^T y=x_1y_1+x_2y_2+...+x_ny_n $$
Now apply the gradient operator
$$ \nabla_x=(\frac{\partial}{\partial x_1},\frac{\partial}{\partial x_2},...,\frac{\partial}{\partial x_n})^T $$
to $x^Ty$ and you get
$$ \nabla_x x^Ty=(y_1,y_2,...,y_n)^T=y $$
So you can usually imagine the vectors to be scalars and apply the standard rules of differentiation.
A: This is just a gradient over a scalar. No matrices are involved here. What you have here is a dot product between a vector and itself. The notation here are not friendly (vector written like a number, e.g. $x_{i}$ is actually the vector $\vec{x}$), but once you figure that out, it shouldn't be complicated.
Remember that $\vec{u}^{T}\vec{v}=\vec{u}\cdot\vec{v}$ (just do regular matrix multiplication to see why), which is just a scalar.
So once you make the notation clearer, the first line is just: $\nabla((\vec{x}-\vec{\mu})\cdot(\vec{x}-\vec{\mu}))$ which is just gradient over the scalar field. Then this can be expanded into second line as $\nabla(\vec{x}\cdot\vec{x}-2\vec{\mu}\cdot\vec{x}+\vec{\mu}\cdot\vec{\mu})$. Now due to linearity of gradient, we have $\nabla(\vec{x}\cdot\vec{x}-2\vec{\mu}\cdot\vec{x}+\vec{\mu}\cdot\vec{\mu})=\nabla\vec{x}\cdot\vec{x}-2\nabla\vec{\mu}\cdot\vec{x}+\nabla\vec{\mu}\cdot\vec{\mu}$ and since $\vec{x}$ is constant, this is just $-2\nabla\vec{\mu}\cdot\vec{x}+\nabla\vec{\mu}\cdot\vec{\mu}$. To see clearly how to get to the third line, write out the dot product in full, that is $\vec{\mu}\cdot\vec{x}=\sum\limits_{i=1}^{n}\mu_{i}x_{i}$ and $\vec{\mu}\cdot\vec{\mu}=\sum\limits_{i=1}^{n}\mu_{i}^{2}$. Hence this become $\sum\limits_{i=1}^{n}\nabla(-2x_{i}\mu_{i}+\mu_{i}^{2})$. And $\frac{d}{d\mu_{i}}(-2x_{i}\mu_{i}+\mu_{i}^{2})=-2x_{i}+2\mu_{i}$ so $\nabla(-2x_{i}\mu_{i}+\mu_{i}^{2})=(-2x_{i}+2\mu_{i})\vec{e}_{i}=-2x_{i}\vec{e}_{i}+2\mu_{i}\vec{e}_{i}$ where $\vec{e}_{i}$ is the unit vector in the $i$-th axis. Hence once we sum up everything, we get $\sum\limits_{i=1}^{n}(-2x_{i}\vec{e}_{i}+2\mu_{i}\vec{e}_{i})=-2\vec{x}+2\vec{\mu}$, which is the third line above.
