I would like to find a rotation matrix between two flats $F\_1,F\_2$ that are defined by the column spaces of the matrices $M\_1,M\_2 \in \mathbb{R}^{n \times k}$ ($k<n$) respectively. If it was to find any rotation matrix, I could do the SVD (Singular Value Decomposition) or Gram-Schmidt process to find the orthonormal bases $U\_1$ of $M\_1$ and $U\_2$ of $M\_2$, and define rotation matrix $R=U\_2 U\_1^{\top}$. But what I need is to find the rotation along the line of intersection $L$. 


Specifically, if $k=2$ and $n=3$, the flats would look something like [this figure](https://www.dropbox.com/s/6o63ugw45hcviev/two_flats.png?dl=0&raw=1). 
I have a vector $v\_1$ lies on $F\_1$, which enters intersection $L$. As it passes through $L$, $v\_1$ is transformed to $v\_2$ such that it lies on $F\_2$. This should not be confused with a projection (My interest is related to finding the shortest path from point on $F\_1$ to $F\_2$.).


Do you have any idea on this problem? One idea I come up with is: 


1. First, find the intersection $L$ by noting that $L$ is a set of points satisfies $P=M\_1 \vec{a\_1}=M\_2 \vec{a\_2}$. i.e., $[M\_1, M\_2][\vec{a\_1}; -\vec{a\_2}]=0$, thus $L$ is the null space of $[M\_1, M\_2]$ ($[\cdot,\cdot]$ and $[\cdot;\cdot]$ are horizontal and vertical concatenation respectively).
2. Find the [angle between flats](https://en.wikipedia.org/wiki/Angles_between_flats) by taking non-zero, non-one singular values of $\bar{U\_1}^\top \bar{U\_2}$, where $\bar{U}\_\cdot\in\mathbb{R}^{n\times k}$ is a thin singular vector matrix (set of singular vectors that have non-zero singular values).
3. Construct rotation matrix using axes and anlge obtained from the previous steps.


However, this method is too cumbersome. I think there is more compact and closed-form solution.



Rotation matrix between two column spaces
Here is a closed-form answer for the generic case with $k=2, n=3$ that makes me doubt there will be a compact answer in general.


Let $p,q$ be vectors perpendicular to $F\_1, F\_2$ respectively. Let $r = p \times q$, where $\times$ is the cross product, so that $r$ is in the intersection of $F\_1$ and $ F\_2$. Then the desired map is


$$v \rightarrow
\frac{(v\cdot p)q}{|p||q|}
+
\frac{(v\cdot r)r}{|r||r|}
+
\frac{(v\cdot (p\times r))(q\times r)}{|p\times r||q\times r|}.
$$


This can be verified by checking that it sends the orthogonal basis $p,r,p\times r$ to the orthogonal basis $q,r,q\times r$, up to some scalar multiples chosen to preserve lengths.


