# Closest 3x3 rotation matrix where all entries are in {1, 0, -1}

Let $$M$$ be a 3x3 right-handed rotation matrix. I need to find a closed form solution for the right-handed rotation matrix $$M'$$ where its entries are lie in $${1, 0, -1}$$.

An initial idea is to replace each row of $$M$$ by the unit norm vector that is collinear to its largest component; i.e if the first row is $$[0.126, -0.946, -0.299]$$, then it should be replaced by $$[0, -1, 0]$$.

Any thoughts on this? Is there a better approach? What if one of the rotation angles is 45 degrees and the max component is not unique?

• There are only 24 possible answers, so a possibility is to brute force it. Undoubtedly there exists something more efficient, but do you care? Anyway, my initial thought is that something like $$\frac13\pmatrix{-1&2&2\cr2&-1&2\cr2&2&-1\cr}$$ could be the worst case from the point of view of breaking ties. – Jyrki Lahtonen Feb 27 at 22:07
• May be there is something quite efficient if we view the rotations in the quaternion space? Quantizing a quaternion to the closest "legal" alternative? Is there a useful description of the Voronoi cells? I need to think about this more. Past midnight here, and I really should hit the sack. – Jyrki Lahtonen Feb 27 at 22:13
• By 'closest' rotation a good definition I think needs not only to produce the closest vectors but also in the closest order. If we only consider cosine distance of vectors to one another, there will always be 3 subsolutions for each solution because we can rotate xyz to zxy to yzx and keep the same vectors with the same handedness – Roulbacha Feb 27 at 23:31

## 2 Answers

To put the problem in its context, this is needed to extract sagittal/axial/coronal slices from MR and CT volumes. The volume files come with rotation matrices that express how the volume is oriented with respect to the standard basis which is the scanner's. Sometimes the slices aren't taken from an orthogonal direction but there's a slight displacement in rotation, so I need to find the closest orthogonal rotation (swap of axis and directions) to know what orthogonal view the volume is taken from. I tried and implemented my solution where I replace each row of my matrix with the unit norm vector on its largest component in absolute value and it works, all my volumes now have an orthogonal rather than oblique rotation.

The columns (or equivalently rows) of any orthogonal matrix are non-zero orthogonal vectors and it is quite easy to take 3 orthogonal vectors with only 1, -1 or 0 as entries. "Closest" is a very vague notion until you specialise some norm on the vector space of 3x3 matrices.

Looking at the original motivation I do not fully understand OP's own answer (what is an orthogonal rotation? For me any rotation is represented by an orthogonal matrix), but maybe this helps: say that the reference frame of the scanner is identified with the canonical basis $$\{e_i\}$$ of $$\mathbb{R}^3$$, that is $$e_1= (1,0,0), \ldots$$. Then, if I understand what the OP wrote, by the usual facts about the matrix associated to a linear map, the $$i$$-th column of your rotation matrix $$R$$ is the vector $$R(e_i)$$, that is the rotated basis vector (more precisely the column gives you the components of $$R(e_i)$$ with respect to the basis $$\{e_i\}$$).

• I want to find the closest (1, -1, 0) rotation, which i call 'orthognal', closest in terms of orientation (cosine similarity) to the basis of the rotated space produced by R (i.e. the rows of the matrix). – Roulbacha Mar 12 at 13:59