Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to create vectors from position data?
Date: Wed, 5 Nov 2008 19:12:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 32
Message-ID: <gesr61$pdp$1@fred.mathworks.com>
References: <gerkmn$sdr$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1225912321 26041 172.30.248.35 (5 Nov 2008 19:12:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Nov 2008 19:12:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:499168


"Dai" <relax2809@hotmail.com> wrote in message <gerkmn$sdr$1@fred.mathworks.com>...
> Hi everyone
> 
> I have exported three dimensional coordinate data of markers placed along either side of a human spine into a text file. Each marker has three coordinates (x,y,z). I attempted to import the text file into MatLab to create vectors connecting each pair of markers and compute the angle of one vector relative to another but could not work out what functions to use. It would be greatly appreciated if anyone could advise me how to create vectors from coordinate data and compute angles thereafter. 
> 
> Thank you in advance
> Dai

  Define position matrix P = [X,Y,Z] where X, Y, and Z are column arrays consisting of the three coordinates of successive data points.  Then do this:

 V = diff(P); % Form 3D vectors between successive points
 V1 = V(1:end-1,:);
 V2 = V(2:end,:);
 % Magnitudes of cross products:
 cr = sqrt((V1(:,2).*V2(:,3)-V1(:,3).*V2(:,2)).^2+...
           (V1(:,3).*V2(:,1)-V1(:,1).*V2(:,3)).^2+...
           (V1(:,1).*V2(:,2)-V1(:,2).*V2(:,1)).^2);
 % Dot products:
 dt = V1(:,1).*V2(:,1)+V1(:,2).*V2(:,2)+V1(:,3).*V2(:,3);
 % Angles between successive vectors:
 A = atan2(cr,dt);

  If there are n points, P will have n rows of the three coordinates.  V will have n-1 rows of vectors between successive points of P.  A will be an array of n-2 angles between the successive vectors of V.  

  Note that cr is an array of the absolute values of the cross products of successive vectors in V, while dt is an array of their successive dot products.

  Note also that angles in A are the angles between successive vectors.  If you want the angles between successive line segments, just use -dt in the atan2 expression.  The one angle is the supplement of the other.

  Finally note that the i_th element of A will be the angle at the i+1_st vertex point of P.  There can be no angle defined at the first and last points of P.

Roger Stafford