Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to create vectors from position data?

Subject: How to create vectors from position data?

From: Dai

Date: 5 Nov, 2008 08:15:19

Message: 1 of 4

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

Subject: How to create vectors from position data?

From: Rune Allnor

Date: 5 Nov, 2008 12:17:41

Message: 2 of 4

On 5 Nov, 09:15, "Dai" <relax2...@hotmail.com> wrote:
> 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.

How to create the vector data depends on how you imported
the (x,y,z) data. If you loaded plain text files, the vectors
are already there, as rows or columns in the matrix you loaded.
If the data were loaded as objects or cell arrays, you need to
consult the dcumentation of the format you use.

As for the angles between vectors, you might want to use the
dot and cross products, see the functions DOT and CROSS.

Rune

Subject: How to create vectors from position data?

From: cpp.matlab@gmail.com

Date: 5 Nov, 2008 15:34:09

Message: 3 of 4

This example will derive the smallest angle in degrees between two
lines in 3D space. The coordinates are in float form in x,y,z format.
For 2D lines, just set the z value to zero. E.g x,y,z = (23,14,0).

Written in c++ Visual Studio 2005, Win32, console application.

It works based on the dot product of two vectos. There is info
on the web about that as it applies to c++. Basically it states
that the dot product of two unit vectors yields the cosine of the
angle between them.

///////////////////////////////////////////////////////////////////////

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers
#include <stdio.h>
#include <tchar.h>
///////////////////////////////////////////////////////////////////////
// AngleBetweenTwoLines.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <math.h>

//Radians to degrees.
#define RTD 57.29577951 // 180/pi

typedef struct fvector_s
{float dx,dy,dz;
}fvector_t;

typedef struct fvertex_s
{ float x,y,z;
}fvertex_t;

float fdot( fvector_t *v1, fvector_t *v2 );
float fAngleDeg(fvector_t *v1, fvector_t *v2 );
float fVectorLength(fvector_t *v);
void fVectorNormalize(fvector_t *v);


//--------------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])
{

fvector_t v1,v2; //two vectors that represent our lines.
fvertex_t p1,p2,p3,p4; // 4 vertices that represent our lines.
float angle_between_lines;

// Make 4 vertices.
//For this example I will just hard code the values here.
p1.x = 0; p1.y = 0; p1.z = 0;
p2.x = 707; p2.y = 707; p2.z = 0;

p3.x = 0; p3.y = 0; p3.z = 0;
p4.x = 200; p4.y = 0; p4.z = 0;

//make two lines (vectors) from these vertices
v1.dx = p2.x - p1.x;
v1.dy = p2.y - p1.y;
v1.dz = p2.z - p1.z;

v2.dx = p4.x - p3.x;
v2.dy = p4.y - p3.y;
v2.dz = p4.z - p3.z;

//get the smallest angle between them.
angle_between_lines = fAngleDeg(&v1,&v2);

printf("The angle between these two lines is %f degrees.\r
\n",angle_between_lines);

return 0;
}


// returns the dot product of two vectors
float fdot( fvector_t *v1, fvector_t *v2 )
{return( (v1->dx*v2->dx) + (v1->dy*v2->dy) + (v1->dz*v2->dz) );}

// returns the length of a vector
float fVectorLength(fvector_t *v)
{return (float)sqrt(fdot(v,v));}

//return the angle in degrees between two lines
float fAngleDeg(fvector_t *v1, fvector_t *v2 )
{
fVectorNormalize(v1);
fVectorNormalize(v2);
return (acos(fdot(v1,v2)) * RTD);
}

// Makes a unit vector, a vector whose length is 1.
void fVectorNormalize(fvector_t *v)
{
float temp;

temp = 1 / fVectorLength(v);

v->dx = v->dx * temp;
v->dy = v->dy * temp;
v->dz = v->dz * temp;
}
///////////////////////////////////////////////////////////////////////

Subject: How to create vectors from position data?

From: Roger Stafford

Date: 5 Nov, 2008 19:12:01

Message: 4 of 4

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us