I would like to solve the following problem in the 'correct' way. I currently use optimization to find the answer, but I would like to get rid of it.

Given a real-valued symmetric Matrix M, containing only elements along all borders and the diagonal.

Also given a symmetric prototype matrix P, containing ones and zeroes. The 1/0 indicate where values are allowed in the result matrix.

I need to find an orthogonal matrix Q, so that when applying

M_2 = Q M_1 Q^-1

the matrix M_2 does not contain any values at the zero positions of P.

The other way is possible, M_2 may contain a zero, where P is one.

M_2(i,j) * (1 - P(i,j)) = 0

P was chosen in such a way, that the transformation is possible.

Q does not have to be unique. Multiple solutions might be possible.

For some forms of P, it is possible to derive Q using a series of Givens rotations, annihilating elements one by one. But I could not find a solution for the general case. Neither could i find a strategy in which order to apply the Givens rotations.

How to derive the Q matrix?

We could start simple:

P is chosen in such a way that Q is unique (I think).

This way M_2 cannot be zero, where P is one.