Rotate a vector until it is perpendicular to another Let $x, y\in\mathbb{R}^n$. I would like to rotate $y$ so that it is perpendicular to $x$. Is it possible to find a matrix $R\in\mathbb{R}^{n\times n}$ that does that?
$$
x^\top Ry = 0
$$
Attempt
The matrix must be orthogonal $RR^\top =I$. This means we must have
$$
\sum_{k=1}^n r_{ik} r_{jk} = \delta_{ij} \qquad \forall i,j\in\{1, \ldots, n\}
$$
Since $RR^\top$ is symmetric, we must have that the above and below diagonal entries are the same, thus they would lead to the same constraints. Overall we have $n$ diagonal and $\frac{1}{2}n(n-1)$ off-diagonal constraints for a total of $\frac{1}{2}n(n+1)$ constraints. In addition, we also have the constraints given by the condition that $y'=Ry$ is perpendicular to $x$
$$
\sum_{i=1}^n\sum_{j=1}^n x_i R_{ij} y_j = 0 
$$
Thus we have $\frac{1}{2}n^2 + \frac{1}{2}n + 1$ constraints. One final constrain, in order for $R$ to be a proper rotation, is given by the determinant $\det(R) = 1$, which gives one more constraint. Overall we have
$$
\frac{1}{2}n^2 + \frac{1}{2}n + 2
$$
constraints. However, $R$ has $n^2$ entries.
 A: First, find the orthogonal projection of $y$ onto the line spanned by $x$ - call it $\hat y$. This amounts to saying $\hat y=u(y\cdot u)$, where $u=x/\|x\|$, the unit vector for that line. The matrix for this is
$$P=\begin{bmatrix}u_1^2&u_1u_2&u_1u_3&\cdots\\u_2u_1&u_2^2&u_2u_3&\cdots\\u_3u_1&u_3u_2&u_3^2&\cdots\\\vdots&\vdots&\vdots&\cdots\end{bmatrix}$$
Such that $\hat y=Py$. Now, the projection onto the orthogonal complement of this is $I-P$. $I$ denotes the identity matrix. From now on, $\hat y$ refers to the orthogonal projection of $y$ onto the orthogonal complement of the line spanned by $x$. This will orthogonally project $y$ onto a line orthogonal to $x$, but we need a rotation of $y$ - we must make it the same length. So we need to rescale by $\|y\|/\|\hat y\|$. The length of this new orthogonal projection, $\hat y=(I-P)y$, is calculated as follows:
$$\|(I-P)y\|=\|y-Py\|=\|y-u(y\cdot u)\|$$
I don’t know how to make a single matrix to accomplish this, but very simply we can say $x^T(I-P)y=0$, and to make this a proper rotation we scale $(I-P)y$ by $\|y\|/\|(I-P)y\|$.
Concrete example:
$$x=\begin{pmatrix}2\\4\\5\end{pmatrix},\,y=\begin{pmatrix}2\\1\\2\end{pmatrix},\|x\|=3\sqrt{5},\,\|y\|=3\\u=\begin{pmatrix}\frac{2}{3\sqrt{5}}\\\frac{4}{3\sqrt{5}}\\\frac{\sqrt{5}}{3}\end{pmatrix}\\P=\frac{1}{45}\begin{pmatrix}4&8&10\\8&16&20\\10&20&25\end{pmatrix}\\I-P=\frac{1}{45}\begin{pmatrix}41&-8&-10\\-8&29&-20\\-10&-20&20\end{pmatrix}\\\hat y=(I-P)y=\frac{1}{45}\begin{pmatrix}54\\-27\\0\end{pmatrix}=\begin{pmatrix}\frac{6}{5}\\-\frac{3}{5}\\0\end{pmatrix}$$
And $\hat y$ is orthogonal to $x$, but is not the same length as $y$, so the rescale is necessary.
A: We are given $x$ and $y$, and we want to find $R$ such that $x^T R y = 0$.  Thus we must have the vector $y_1 = R y$ in the orthogonal space of vector $x$.
So the first step is to generate the orthogonal space to $x$ by solving the homogenous linear system $ x^T v = 0 $ for all possible $v$'s.  Let the matrix $V$ contain a basis of this orthogonal space, then we can select
$y_1 = V u $
for some $u \in \mathbb{R}^{n-1} $
Since rotation preserves length, then we have to scale $y_1$ to have the same length as $y$.
Next, the rotation matrix $R$ alters $y$ into $y_1 = R y$ but does not alter the vectors orthogonal to $y$ and $y_1$, so the next step is to find an orthogonal basis for the orthogonal complement to the two vectors {$y$, $y_1$}, and this can be done in two steps, first find a basis (not necessarily orthogonal) of the orthogonal complement to {$y$, $y_1$}, which will have dimension $n-2$.  In step two, you'll take these $n-2$ vectors and ammend them with the vectors $y$ and $y_1$, so that now you have a complete set of $n$ vectors.  Run the Gram-Schmidt process to orthogonalize them, and let the matrix containing the orthonormal vectors be $G$. Now express $y$ and $y_1$ in terms of these basis vectors.
$y = G w_1 $ and $y_1 = G w_2 $
Since the first $(n-2)$ vectors of $G$ are orthogonal to $y$ and $y_1$, then the first $(n-2)$ entries of $w_1$ and $w_2$ will be zero.
Let $p_1$ be the $2 \times 1$ vector of the last two entries of $w_1$ and $p_2$ be the $2 \times 1$ vector of the last two entries of $w_2$, then we can relate $p_2$ to $p_1$ by a rotation (remember they have the same length),
$p_2 = R_{22} p_1$
where $R_{22}$ is a $2 \times 2$ rotation matrix.  Finally define the matrix $E $ by
$ E = \begin{bmatrix} I_{(n-2) \times (n-2)} && 0_{(n-2) \times 2} \\ 0_{2 \times (n-2)} && R_{22} \end{bmatrix} $
Then we have $w_2 = E w_1 $
Recalling the $y= G w_1 $ and $y_1 = G w_2$ then we can write
$y_1 = G E w_1 = G E G^T y $
Hence the rotation matrix relating $y$ and $y_1$ is $R = G E G^T$
Thus, there are infinite rotation matrices $R$ that can rotate a given vector $y$ to become orthogonal to another given vector $x$.
A: Not too familiar with matrix algebra but your first problem I can certainly solve. The answer is:
$$ \frac{y \times x}{|x| \sin \theta}$$
You may check that this is the result expression above can be geometrically visualized as 'twisting' $y$ ninty degrees about the line define by $x$ after killing of the component of $y$ along $x$. To restore the length to the original case we bring in the scaling factor in the denominator.
For the case of matrix, we can consider this rotation aspect of cross product to be realized as a matrix by the method described here
