TITLE: Minimize the Frobenius norm of the difference of two matrices with respect to matrix: $\underset{B} {\mathrm{argmin}} \left\| A- B \right\|_F$
QUESTION [8 upvotes]: The following question is similar to this one, but I think that it is not straightforward to move from one to the other, so please take a look. Otherwise, please let me know and I will delete it.

Let $A,B\in\Bbb{R}^{n\times n}$ two square real $n\times n$ matrices with the additional properties that $A$ is also symmetric, and $B$ is diagonal with entries $\{b_i\colon b_i\in\Bbb{R}, i=1,\ldots,n\}$, that is, $B=\operatorname{diag}(b_1,\ldots,b_n)$.
We want to minimize the Frobenius norm of the difference of $A$, $B$ with respect to the matrix $B$. Let $B^{\star}$ denote the minimizer of the aforementioned norm, that is
$$
B^{\star} = \underset{B} {\mathrm{argmin}} \left\| A- B \right\|_F,
$$
where $\lVert A\rVert_F$ denotes the Frobenius norm of an $n\times n$ real symmetric matrix $A=\big(a_{ij}\big)_{i,j=1}^n$, and is given by
$$
\lVert A\rVert_F
=
\left(\sum_{i,j=1}^n \left\| a_{ij} \right\|^2\right)^{\frac{1}{2}}
=
\sqrt{\operatorname{tr}(A^\top A)}
=
\sqrt{\operatorname{tr}(AA^\top)}.
$$

REPLY [5 votes]: You can write the objective function in terms of the Frobenius product, then it is a simple matter to find its differential and gradient 
$$\eqalign{
 f &= (B-A):(B-A) \cr
 df &= 2(B-A):d(B-A) \cr
    &= 2(B-A):dB \cr
 \frac{\partial f}{\partial B} &= 2(B-A) \cr
}$$
Note that the above gradient is for the general case, with no constraints on $B$.
The diagonal constraint can be enforced by Hadamard multiplication with the identity matrix 
$$\eqalign{
 \frac{\partial f}{\partial B} &= 2I\circ(B-A) \cr
     &= 2\,(B - I\circ A) \cr
}$$
Setting the constrained gradient to zero and solving for $B$ yields
$$\eqalign{
 B &= I\circ A \cr
}$$
To address the question in your comment, where $B$ is constrained to be a scalar multiple of the identity, we have 
$$\eqalign{
  B &= bI \cr
 dB &= I\,db \cr
}$$
Substitute these into the unconstrained differential expression 
$$\eqalign{
 df &= 2(B-A):dB \cr
    &= 2(bI-A):I\,db \cr
    &= 2\,\big(b\,{\rm tr}(I)-{\rm tr}(A)\big)\,db \cr
 \frac{\partial f}{\partial b} &= 2\,(bn-{\rm tr}(A)) \cr
}$$
Setting this gradient to zero and solving for the scalar $b$ yields
$$\eqalign{
 b &= \frac{{\rm tr}(A)}{n} \cr
}$$