$$\begin{array}{ll} \text{minimize} & \| A - B \|_F^2\\ \text{subject to} & B x = v\end{array}$$
where $B$ is an $m \times n$ matrix and $x$ is an $n$-vector where each element is $1/n$ (an averaging vector). In layman terms, I want to find the 'closest' matrix to $A$ that has a new average along the rows.
Now I might be completely off as this is the first time trying to solve such a problem but I thought I could do something like this. Please take me through the final steps and if my idea is completely wrong let me know why and what I should be doing.
Here is my attempt so far:
$$\text{Trace}\left[(A-B) (A-B)^{\mathsf{T}}\right]$$
I was hoping I could use the Lagrange method.
These are the identities I have found useful:
$$\frac{\partial \text{Trace}[x]}{\partial x}=\text{Trace}\left[\frac{\partial X}{\partial x}\right]$$
$$\text{Trace}[A+B]=\text{Trace}[A]+\text{Trace}[B]$$
$$\frac{\partial X^{\mathsf{T}}}{\partial x}=\left(\frac{\partial X}{\partial x}\right)^{\mathsf{T}}$$
So this is the problem I thought could be the way to solve it:
$$\text{Trace}\left[(A-B) (A-B)^{\mathsf{T}}\right]-\lambda (B x-v)$$
Finding the gradient(please let me know if I am wrong) and setting it to zero and solving for transpose of B I get:
$B^{\mathsf{T}}=A^{\mathsf{T}}+\frac{\lambda x}{2}$
However, I belive that what I have done is wrong or there is something I am missing because now B is a matrix + a vector as far as I can see, so the dimensions does not work.
Unfortunately I am out of my depth and I have started to read about optimization and how to solve similar problems but I need a answer to this quickly.
I am happy for any comments, improvements corrections that would make this useful for other people.
Thank you very much for any feedback!
The beginning looks fine. However, note that you need one Lagrange multiplier per constraint. Thus you need a vector $\lambda$.
The function to minimize is then $$\operatorname{tr}(A^T-B^T)(A-B) - \lambda^T (B x -v). $$
Taking the gradient with respect to $B$, we arrive at $$2 (B-A)- \lambda x^T =0. $$
We can solve this for $B$ with the result $$ B = A + \frac12 \lambda x^T\;.$$
The Lagrange multiplier have to be determined such that the constraints are fulfilled, e.g., $$B x = Ax + \frac12 \lambda x^Tx = v\;.$$ This leads to $$ \lambda = \frac{2}{x^T x}(v -Ax)$$ and thus to the explicit solution $$ B = A + \frac{1}{\Vert x \Vert^2} (v -Ax) x^T $$