<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213</link>
    <title>MATLAB Central Newsreader - Directional Cosine Matrix</title>
    <description>Feed for thread: Directional Cosine Matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 19 Jun 2008 13:49:01 -0400</pubDate>
      <title>Directional Cosine Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213#438337</link>
      <author>Chia Khai Low</author>
      <description>I have two vectors in different excel files. I have &lt;br&gt;
successfully imported them and matched them by date. These &lt;br&gt;
3d vectors are in cartesian coordinates. I want to find the &lt;br&gt;
directional cosine matrix to convert the first vector into &lt;br&gt;
the second vector.&lt;br&gt;
&lt;br&gt;
Anyone with the method please post it here or show me links &lt;br&gt;
on the math behind it. Once I have this DCMs(for every &lt;br&gt;
instance) I can make Euler angles, visualizations and stuff.&lt;br&gt;
&lt;br&gt;
Thanks and bless you.&lt;br&gt;
&lt;br&gt;
ps. reply quickly :)</description>
    </item>
    <item>
      <pubDate>Fri, 20 Jun 2008 06:54:02 -0400</pubDate>
      <title>Re: Directional Cosine Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213#438547</link>
      <author>Roger Stafford</author>
      <description>&quot;Chia Khai Low&quot; &amp;lt;kaiserwulf@gmail.com&amp;gt; wrote in message &amp;lt;g3do4d$667&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I have two vectors in different excel files. I have ........&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;It is not entirely evident from your description what you asking, Chia.  For &lt;br&gt;
example, the statement, &quot;These 3d vectors are in cartesian coordinates&quot; is not &lt;br&gt;
completely clear to me.  I am guessing what you mean is that you have two n &lt;br&gt;
by 3 arrays, A and B, in which the corresponding rows in them are x,y,z &lt;br&gt;
(Cartesian) coordinates in two different coordinate systems, one presumably &lt;br&gt;
rotated with respect to the other about a common origin.  It is this 3 x 3 &lt;br&gt;
rotation matrix that you are apparently seeking as your &quot;directional cosine &lt;br&gt;
matrix&quot;.  Have I guessed correctly?&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You didn't mention a possible translation in aligning the two sets of points.  &lt;br&gt;
If that were necessary, you could subtract the mean values of each so that the &lt;br&gt;
sum of the coordinates in A and B would then be zero.  The necessary &lt;br&gt;
translation would then be the vector difference between these two mean value &lt;br&gt;
vectors.  I have therefore assumed below that the sums of each coordinate &lt;br&gt;
column in A and B are zero: sum(A,1) = sum(B,1) = [0,0,0].&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I do not regard the method I am about to give as entirely satisfactory.  It has &lt;br&gt;
two fundamental weaknesses.  The first weakness is that if A and B are not &lt;br&gt;
accurate rotations of one another, the method may not give a perfect least &lt;br&gt;
squares optimum rotation matrix for them.  In fact, if they vary too far from &lt;br&gt;
being rotations of one another, the method could conceivably fail altogether.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The second weakness has to do with the eigenvalues (singular values in this &lt;br&gt;
case) that are computed below.  If it were to happen that two or three of them &lt;br&gt;
were essentially equal, the method again falls apart.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If we (temporarily) ignore these difficulties, do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[U1,S1,V1] = svd(A'*A,0); % Find eigenvalues and eigenvectors&lt;br&gt;
&amp;nbsp;[U2,S2,V2] = svd(B'*B,0); % for A'*A and B'*B&lt;br&gt;
&amp;nbsp;mn = inf;&lt;br&gt;
&amp;nbsp;II = [-1,-1,-1;-1,-1,1;-1,1,-1;-1,1,1;1,-1,-1;1,-1,1;1,1,-1;1,1,1];&lt;br&gt;
&amp;nbsp;for k = 1:8&lt;br&gt;
&amp;nbsp;&amp;nbsp;Rt = V1*diag(II(k,:))*V2'; % One of these eight&lt;br&gt;
&amp;nbsp;&amp;nbsp;X = B-A*Rt; %  possibilities should be the right one&lt;br&gt;
&amp;nbsp;&amp;nbsp;tt = norm(X(:)); % Choose the closest fit to find it&lt;br&gt;
&amp;nbsp;&amp;nbsp;if tt &amp;lt; mn, mn = tt; R = Rt; end&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
Then R is the desired rotation matrix from the n points in A to those in B.  We &lt;br&gt;
should have essential equality, B = A*R, provided the points in B are actually &lt;br&gt;
rotated versions of those in A.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The reasoning here is that the eigenvalues of A'*A and B'*B should &lt;br&gt;
(hopefully) be the same, and then the matrix V1*V2', with one of eight &lt;br&gt;
possible combinations of sign changes in the columns of V1, should be the &lt;br&gt;
required transformation from A to B.  We have only to search through the &lt;br&gt;
eight possibilities to find it.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;As stated above, if two or three of the eigenvalues in S1 or S2 should &lt;br&gt;
happen to be equal, the logic in the previous paragraph fails.  There would &lt;br&gt;
have to be a search through infinitely many possible matrices between V1 and &lt;br&gt;
V2', which of course is impossible.  The method would break down in this &lt;br&gt;
case.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I should mention here that if some kind of reflection has occurred between &lt;br&gt;
the points of A and those of B, this method will come up with an R matrix &lt;br&gt;
whose determinant is -1, that is, it is a reflection, not a rotation, matrix.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I am sure there exists a superior method of solving your problem that would &lt;br&gt;
avoid the two aforementioned weaknesses, but I have not been able, up to &lt;br&gt;
this time, to come up with a practical one.  If I manage to think of a better &lt;br&gt;
method, I will inform you, or perhaps some other kind soul will present one &lt;br&gt;
here.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 20 Jun 2008 15:55:05 -0400</pubDate>
      <title>Re: Directional Cosine Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213#438651</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
message &amp;lt;g3fk6a$co7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;   It is not entirely evident from your description what you asking, Chia.&lt;br&gt;
&amp;gt; ........&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;A good night's sleep has furnished a much superior method of solving your &lt;br&gt;
problem, Chia.  This method has none of the disadvantages of the one I sent &lt;br&gt;
you yesterday and is much simpler.  There is no need to run through the &lt;br&gt;
previous eight possibilities.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Assume, as before, that A and B are n x 3 arrays in which all column sums &lt;br&gt;
are zero.  Then do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[U,S,V] = svd(A'*B);&lt;br&gt;
&amp;nbsp;R = U*V';&lt;br&gt;
&lt;br&gt;
This R will be your desired unitary (direction cosine) matrix that, if applied to &lt;br&gt;
A, will bring you to B:  B = A*R.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If B differs from such a transformation of A, then this solution yields an R &lt;br&gt;
which I have reason to believe, (but can't prove,) is the best one in a least &lt;br&gt;
squares sense.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;In the case that two or all three of the singular values in S are equal, there &lt;br&gt;
will be an inherent indeterminacy, but this time it does not interfere with &lt;br&gt;
finding a valid solution.  In such cases there would be an infinite continuum &lt;br&gt;
of equally good solutions.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Again I remind you that the above solution, R, can in some cases have a &lt;br&gt;
determinant of -1 instead of +1 and therefore be a reflection, rather than &lt;br&gt;
rotation, matrix.  In these cases the reflection matrix would provide a closer &lt;br&gt;
fit than a true rotation matrix.  It would nevertheless be unitary, that is, a &lt;br&gt;
matrix of direction cosines.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 20 Jun 2008 21:47:02 -0400</pubDate>
      <title>Re: Directional Cosine Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213#438731</link>
      <author>Chia Khai Low</author>
      <description>Dear Roger,&lt;br&gt;
&lt;br&gt;
Thank you so much for your comprehensive and yet quick &lt;br&gt;
reply. And the follow-up too.&lt;br&gt;
&lt;br&gt;
Have a great day.&lt;br&gt;
&lt;br&gt;
Thanks again&lt;br&gt;
&lt;br&gt;
Chia</description>
    </item>
    <item>
      <pubDate>Mon, 23 Jun 2008 01:31:02 -0400</pubDate>
      <title>Re: Directional Cosine Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/171213#438958</link>
      <author>Roger Stafford</author>
      <description>&quot;Chia Khai Low&quot; &amp;lt;kaiserwulf@gmail.com&amp;gt; wrote in message &amp;lt;g3h8gm$jd4&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thank you so much for your comprehensive and yet quick .........&lt;br&gt;
&amp;gt; Chia&lt;br&gt;
&lt;br&gt;
Hello Chia,&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;This is to let you know that I am now able to prove rigorously that the solution &lt;br&gt;
I gave you in the second of my articles in this thread does in fact provide the &lt;br&gt;
best orthogonal (rotation/reflection) matrix R in the least squares sense between &lt;br&gt;
one set of points and another, which means that you can now use this method &lt;br&gt;
with confidence.  The proof involves the Schwartz-Cauchy inequality.  As you &lt;br&gt;
may recall, I was unable to come up with a proof earlier.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I hope for my peace of mind you will completely abandon the method I &lt;br&gt;
proposed in the first article involving a search through eight possibilities.  That &lt;br&gt;
is not the right way to do the problem.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
  </channel>
</rss>

