Let $A, B, C$ be known matrices, and let $X$ be an unknown matrix.


Given that $C = AXB \Leftrightarrow \text{vec}(C) = K \text{vec}(X)$, where $\text{vec}(\cdot)$ denotes the vectorization of a matrix, $K = (B^{T} \otimes A)$ and $\otimes$ is the Kronecker product, it is easy to infer the least squares solution for $X$:


$$\arg\min\_{X} || A X B - C ||^{2} = (K^{T} K)^{-1} K^{T} \text{vec}(C).$$


Is there a closed form solution also for the case in which $X$ is a diagonal matrix, i.e. for the following problem?


$$\arg\min\_{X = diag(x\_{1}, \ldots, x\_{n})} || A X B - C ||^{2}.$$


Thank you guys in advance for your time :)



arg min_X ||A X B - C||^2, with X diagonal
Yes, it is possible. Notice that $AXB$ is a linear function in each $x\_i$, so you can write it as $Mx$, for some $M\in\mathbb{C}^{n^2\times n}$. With a little work one can find out that the $i$th column of $M$ is given by the Kronecker product of the $i$th column of $A$ and the $i$th row of $B$, so one can write this matrix explicitly (if needed). If I am not mistaken, this can be written as $(B\otimes \mathbf{1})\circ(\mathbf{1}\otimes A)$, where $\otimes$ is the Kronecker product, $\circ$ is the Hadamard product, and $\mathbf{1}\in\mathbb{R}^{n}$ is the vector of all ones.


So you can write your problem as the standard least-squares problem
$$
\arg \min\_{x\_1,x\_2,\dots, x\_n} ||Mx-\operatorname{vec}(C)||^2,
$$
which is even easier to solve in practice than the original one, since $M$ has a small dimension $n$. A closed form solution is $(M^TM)^{-1}M^T\operatorname{vec}(C)$.


