# How to accurately find angle between these two vectors ? (Data Provided).

3 views (last 30 days)

Show older comments

Hi Everyone,

I am struggling so hard to accurately find angle between two vectors. I don't know where I am doing mistake but the angle result of cos(theta) from formula gives incorrect angle.

If you will be asking where I know that it is incorrrect, I know it because I am using rotation matrix to rotate one of them along Z-axis to fit one vector to another.

I basically want to rotate vector1 x,y,z with the angle found between vector1 and vector2.

Here is the angle I am trying to find.

and actually finding it with formula.

Angle Formula :

Rotation Formula along z-Axis (CLOCKWISE) :

|x cos(-θ) − y sin(-θ)| |x'|

|x sin(-θ) + y cos(-θ)| = |y'|

| z | |z'|

Graph rotates respectively and without deformation but much more than the found cousine value.

AFTER ROTATION:

Data

All answers are welcomed

##### 5 Comments

Image Analyst
on 26 Jul 2020

### Accepted Answer

Matt J
on 26 Jul 2020

Edited: Matt J
on 26 Jul 2020

function [theta, Nrm]=vecrot(vstart,vend)

%Find rotation carrying one vector toward another about their common perpendicular

%axis.

%

%IN:

%

% vstart: Initial vector, 3D

% vend: Final vector, 3D

%

%OUT:

%

% theta: the rotation angle in degrees

% Nrm: the rotation axis, directed so that rotation is clock-wise by theta about Nrm

vstart=vstart(:)/norm(vstart);

vend=vend(:)/norm(vend);

Nrm=cross(vstart,vend);

b=vend.'*vstart;

theta = atan2d(sqrt(1-b^2),b);

##### 5 Comments

Matt J
on 26 Jul 2020

Edited: Matt J
on 26 Jul 2020

Since theta is in degrees, you should be using sind and cosd. Also, the rotation formula that you are using assumes inappropriately that Vector1 and Vector2 both lie in the xy-plane, and therefore that the rotation is about the z-axis. This is clearly not true:

load('MyData.mat')

Old=[VRDataX,VRDataY,VRDataZ].';

New=[NewMocapX,NewMocapY,NewMocapZ].';

Vector1=Old(:,end),

Vector2=New(:,end),

Vector1 =

149.3790

7.3849

4.0263

Vector2 =

149.3610

0.1810

3.8730

The actual rotation axis is returned in the Nrm output argument from the code I gave you. You can perform the rotation about this axis (or any other), using my AxelRot utility on the File Exchange

OldRotated=AxelRot(Old,theta,Nrm,[]);

hold on

scatter3(Old(1,:),Old(2,:),Old(3,:),'MarkerFaceColor','g','MarkerEdgeColor','none')

scatter3(New(1,:),New(2,:),New(3,:),'MarkerFaceColor','r','MarkerEdgeColor','none')

scatter3(OldRotated(1,:),OldRotated(2,:),OldRotated(3,:),'MarkerFaceColor','b','MarkerEdgeColor','none')

hold off

legend('Old','New','Old (Rotated)')

The alignment is not as good as my other answer, because the single-angle calibration model is not good (or so it seems). It ignores origin shift between the two data sets.

### More Answers (1)

Matt J
on 26 Jul 2020

Edited: Matt J
on 26 Jul 2020

I don't know what your ultimate purpose is with this rotation you are trying to perform. If the goal is to make the two loci overlap as much as possible, I think you should be doing more than just a one degree-of-freedom angle match-up. I think you should be doing a full 6 degree-of-freedom rigid registration. You can do this with absor from the File Exchange, which for me gives pretty good overlap:

load('MyData.mat')

Old=[VRDataX,VRDataY,VRDataZ].';

New=[NewMocapX,NewMocapY,NewMocapZ].';

[reg,OldRotated,err]=absor(Old,New);

hold on

scatter3(New(1,:),New(2,:),New(3,:),'MarkerFaceColor','r')

scatter3(OldRotated(1,:),OldRotated(2,:),OldRotated(3,:),'MarkerFaceColor','b')

hold off

legend('New','Old (Rotated)')

##### 6 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!