I'm currently working with an accelerometer data vector (x,y,z), where I want to "eliminate" the y component of the data while not scaling the vector. I want to do this by rotating my vector around the x-axis, as it lends to a good result later on in my calculations.
The rotation is done using the following relation: $$ \begin{bmatrix} u \\ 0 \\ v \\ \end{bmatrix} = \begin{bmatrix} 1& 0& 0\\ 0& z~d& -y~d\\ 0& y~d& z~d \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix}$$
Where: $\begin{bmatrix} x \\ y \\ z \end{bmatrix}$ is the matrix to be rotated, $\begin{bmatrix} u \\0 \\v \\ \end{bmatrix}$ is the rotated matrix and $d \triangleq \frac{\sqrt{y^2+z^2}}{y^2+z^2}$
However, after implementing the rotation in code, I have come to realise, that I don't always end up with the "shortest" rotation to the xy-plane, though this is what I want. Can somebody suggest a way to ensure I get the shortest rotation? I realise that an easy fix would simply be to check if z is positive or negative then assigning the sign to v accordingly, but I was hoping for a more elegant solution.
The rotation realtion is derrived as follows.
Starting from a stanard rotation matrix: $$ R_x = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) \\ 0 & -\sin(\theta) & \cos(\theta) \end{bmatrix} \triangleq \begin{bmatrix} 1 & 0 & 0\\ 0 & a & b\\ 0 & -b & a \end{bmatrix} $$ I know that i want to keep x unchanged while removing the y component: $$ \begin{bmatrix} 1 & 0 & 0\\ 0 & a & b\\ 0 & -b & a \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} x \\ ay + bz \\ -by+az \end{bmatrix} = \begin{bmatrix} u \\ 0 \\ v \end{bmatrix} $$ I know that a rotation percerves lenght $u^2 + v^2 = x^2 + y^2 + z^2$ implying $v = \sqrt{y^2+z^2}$
From this i can find $a$ and $b$: $$ \begin{align} v = \sqrt{y^2 + z^2} &= -by + az \text{ and } 0 = ay+bz \\ & \Updownarrow\\ a&= z~\frac{\sqrt{y^2+z^2}}{y^2+z^2}\\ b &= -y \frac{\sqrt{y^2+z^2}}{y^2+z^2} \end{align} $$
leading to:
$$ \begin{bmatrix} u \\ 0 \\ v \\ \end{bmatrix} = \begin{bmatrix} 1& 0& 0\\ 0& z~d& -y~d\\ 0& y~d& z~d \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix}$$